PHP - Get Substring By Referencing Other Substrings Around It
Hello,
Does anyone know how to get a substrig by referencing other substrings around it? Ie, my string is the following: These are my favorite foods: <ul> <li id="1"> Pizza </li> <li id="2"> Burgers </li> <li id="three"> Hot Dogs </li> </ul> I need to get the ID of each list item, i know they will always follow "<li id=". I also need to get the text of each list item, i know they will always be before "</li>" Thanks! Similar TutorialsHi is there anyway to check a string for any instances of something like this "(3)" where the number "3" could be any integer. So basically I want to search a string for any instances of a integer in brackets "(integer)" and remove it. So my string was something like $string = "String (2) string string string (7)" function($string) output would be "String string string string" where there integers in brackets could be any number hey guys, i have my code set up to run substrings through a db to check for matches...even when there is a match it wont insert correctly Code: [Select] $result = mysql_query("select * from friends where userid=$userid"); while($row = mysql_fetch_assoc($result)) { $first_name = $row['first_name']; $last_name = $row['last_name']; $strpos_1 = strpos($friend_1, ' '); $substr_1 = substr($friend_1, 0, $strpos_1); $substr_2 = substr($friend_1, $strpos_1); if($substr_1 == $first_name && $substr_2 == $last_name) { $sql = "insert into fav_friends values('', '$userid', '$friend_1', '$friend_2', '$friend_3', '$friend_4', '$friend_5', '$friend_6')"; $query = mysql_query($sql); } } and i don't receive any errors, any thoughts? Hi, how do I extract a substring? I just want to store "car" in a variable for other uses: Here is the original string: car/red/blue/gun/hit Any help much appreciated! So I have a database with a bunch of dates in it. In this format 05/15/2011. And this is how I get that out. $data = mysql_query("SELECT * FROM location ORDER BY id ASC") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { $date = $info['date']; }; echo "$date"; The resulted echo would be 05/26/2011 How do I get $date to echo out JUST 05? I'm trying to isolate the month. I think the method is using substrings, but I havent had any luck. Thanks any help would be great! I am using tinyMCE editor and uploading image from it.After form is submitted,it makes textarea's values as <img width="\"65\"" height="\"66\"" alt="\\"" src="\"images/logo.gif\""> cuz of wrong html,it is not displaying image.I tried to replace $_POST['elm2'] = str_replace('"\"','\"',$_POST['elm2']); $_POST['elm2'] = str_replace('\""','\"',$_POST['elm2']); but it is not replacing.How can i correct the HTML so that i display image. Folks, I want to extract something from a URL, i think we need to use regex for this. Example URL: Code: [Select] http://www.amazon.co.uk/gp/aw/d/B004JN01LW/ref=mp_s_a_1?qid=1305169492&sr=8-1 What we need to Extract: So we need to extract B004JN01LW from the URL. I tried to use substr() but i think its not useful for this purpose, can someone help me with Regex to extract this? Cheers Natasha Let's say I have the string "Bob is my friend. Bob likes to eat apples. Goodbye!". I give a function that string, and the two strings "Bob" and "apples". It returns "Bob likes to eat apples". I seem to have trouble making this function. If I wanted it to return "Bob is my friend. Bob likes to eat apples" that would be much simpler, but I am trying to get the smallest amount of characters between the two strings. I tried doing this, but seem to either get stuck on an infinite loop or return the entire inputted string. Code: [Select] function between($strIn, $str1, $str2){ $pos2 = stripos($strIn, $str2); $pos = 1; $strStore = ""; while($pos>0 && $pos<$pos2){ $pos = stripos($strIn, $str1); $strStore = substr($strin, $pos, $pos2-$pos); $strIn = substr_replace($srtIn, $strtemp, ""); } return $strStore; } What it does it finds the first occurrence of "apples" ($str1) and then keeps finding and deleting every occurrence of "Bob" ($str2) until the position of "Bob" surpasses the position of "apples". As you can tell, it doesn't work. Could anyone help me out, or tell me what I am doing wrong? This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=308768.0 Hey all, I have method like this: Code: [Select] echo tinymce_tag($post->body); Basically the body property contains a string from database containing one or many paragraphs delimited by p tags: Code: [Select] <p>Hello World.</p> <p>Goodbye World.</p> I want to give all the paragraphs a class. This function will give the first p tag a class: Code: [Select] if ( ! function_exists('tinymce_tag')){ function tinymce_tag($content = ''){ $pos = strpos($content, '<p>'); if($pos !== false){ $content = substr_replace($content,' class="paragraph_styler"', 2, 0); } return $content; } } But I am not sure how to give all the p tags in the content variable a class. Thanks for response. I know you can use str_replace to replace a value with another value, or an array of values with an array of other values. What I'd like to do is replace each instance of a single value with an array of values which get iterated through for each instance found. Think prepared statements that you don't have to call setString or setInt on. Here's a made up example. Code: [Select] $db->query = 'SELECT * FROM users WHERE user_name = ? AND user_role = ?; $db->params = array('hopelessX','admin'); $db->executeQuery(); //this contains a call to a private function that does the replace I have everything else working except this. It works if I specify an array of values to replace and do something like below, but I was hoping to not have to support that method of replace if I didn't need to. Code: [Select] $db->query = 'SELECT * FROM users WHERE user_name = :name AND user_role = :role; $db->replace = array(':name',':role'); $db->params = array('hopelessX','admin'); $db->executeQuery(); //this contains a call to a private function that does the replace I can do it that way if I need to or I could make some overly complicated thing that finds each instance of the question mark, replaces it with a unique value and then adds that unique value to a replace array similarly to the method in code block two but that defeats to the purpose of making this simple database adapter in the first place (although it was mostly just for fun anyway). Anyway, if anyone has any advice and knows of a simple means to achieve this goal that would be much appreciated. Thanks in advance This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=321386.0 Hi Is it possible in PHP to reference a dll, I have seen a few things that suggest this is possible but nothing that shows a simple example. thanks I haven't posted here before but I am completely stuck. I've checked everywhere and cannot find answer to this question. I have an array and if I were to do this: echo $partNames[$jjk]; it would return another part of an array as such: $decoded[0]["Parts"][3]["BodyPart"] (if I retrieve type, this is a string). Now, I need to return the actual value from particular array reference and I cannot for the life of me do it and its destroying my will to live. i've tried combinations of: echo $partNames[$jjk]; echo $$partNames[$jjk]; echo ${$partNames[$jjk]}; echo ${$partNames}[$jjk]; $sssttt = substr($partNames[$jjk], 1); echo $sssttt.' - '.${$sssttt}; Yet nothing seems to work. Solution is to rewrite whole script but this is now pretty complicated parser and I do not want to rewrite sections as deadlines are looming. Any help would be appreciated. (I'm new but i'll try to help others now in anticipation of help). Cheers Here's where I am thus far: This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=322595.0 Hi guys, As a uni student i have to reference alot and my uni has its own prefered system. I would like to make referencing as easy as possible. http://library.scotch.vic.edu.au/research/biblios/ScotchBib/index.htm has a neat system that makes referencing quite easy. My question is how would i start the coding. When the form is sent all it does is string together the inputed text and numbers. What would this code look like and how could i display it in a 'Select All' Box? This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=325339.0 Hi I am in the process of converting to Object Oriented from Procedural. To cater for this I have built an admin_login function, contained within a class: 'siteFunctions'. However, I am having trouble pointing the admin form to the function correctly. Every time I click 'submit', the form does not process anything. It doesn't even 'think' about it i.e. show the egg timer.... I have built this script heaps of times using the procedural method, so I guess I am somehow doing something wrong with respect to referencing the action attribute of the form (due to my new approach). I am very new to OO so please go easy on me: I know the script isn't particularly advanced. I just want to get used to putting functions into classes, and then calling the code, before I move onto more advanced stuff. I have placed all of the files within the same folder in order to rule out driectory path issues. Here are the three scripts that I think are relevant (login, functionsClass, and the mysql connection script): Login $pageTitle = "Admin Login"; include("admin_header.php"); include_once("sitefunctions.php"); new siteFunctions(); echo '<div class="admin_main_body">'; <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]);?>" method='post'> <input type="text" name="username" id="username" size="20"> <label>Username</label><br /> <input type="password" name="password" id="password" size="20"> <label>Password</label><br /> <input type="submit" name="submit" id="submit" value="submit"> </form> echo '<div>'; include("includes/admin_footer.php"); sitefunctions.php //$page = "admin_index.php"; class siteFunctions { var $message; function admin_login() { echo '<div class="admin_main_body">'; $message = NULL; if (isset($_POST['submit'])) { require_once ("mysql_connect.php"); if (empty($_POST['username'])) { $u = FALSE; $message .= '<p> Please enter your username </p>'; } else { $u = escape_data($_POST['username']); } if (empty($_POST['password'])) { $p = FALSE; $message .= '<p>You forgot to enter your password </p>'; } else { $p = escape_data($_POST['password']); } if ($u && $p) { // If everything's OK. $query = "SELECT * FROM admin WHERE username= ('$u') AND password=('$p')"; $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); $row = mysqli_fetch_array($result, MYSQLI_BOTH); if ($row) { session_start(); $_SESSION["admin_id"] = $row[0]; //header("$page"); //Redirects user to admin_index.php //header('location: "$page"'); header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "admin_index.php"); //echo '$_SESSION["admin_id"]'; } else { $message = '<p> The username and password combination are incorrect.</p>'; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } else { $message .= '<p>Please try again.</p>'; } } if (isset($message)) { echo '<font color="red">', $message, '</font>'; } //$adminLogin = 'admin_login'; } //Closes function } //Closes class Connection Script // This file contains the database access information. This file also establishes a connection to MySQL and selects the database. // Set the database access information as constants. DEFINE ('DB_USER', 'atkinson'); DEFINE ('DB_PASSWORD', 'XYZ111WA'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'practicesite'); if ($dbc = @($GLOBALS["___mysqli_ston"] = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD))) { // Make the connnection. if (!((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . constant('DB_NAME')))) { // If it can't select the database. // Handle the error. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not select the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); // Print a message to the user, include the footer, and kill the script. echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of mysql_select_db IF. } else { // If it couldn't connect to MySQL. // Print a message to the user, include the footer, and kill the script. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not connect to the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of $dbc IF. // Function for escaping and trimming form data. function escape_data ($data) { global $dbc; if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } return mysqli_real_escape_string( $dbc, trim ($data)); } // End of escape_data() function. Any help would be appreciated. Cheers Will Hi, I have an html page with multiple image tags inside it. I'm wanting to get the full URL of a specific image within that file. The specific image I'm looking for must start with 'http://www.mysite.com/images/' in the src part of the tag to make sure I'm getting the correct image. To find the occurence, I'm using strpos() to find the character position of the string match. What I then need to do is complete the rest of the URL to get the image name and then output this. So basically I need to get the string that comes after 'http://www.mysite.com/images/' and end when the speech mark occurs to close the src part of the image tag. This will then give me the full URL of my image. How do I do this? Or is there a better way to do this? Thanks. If I explode a string well using a substring when I implode that substring is now missing. for example. Code: [Select] $exploded = explode("[MAC]",$data); $data = implode($exploded); Now [MAC] would be missing in the $data string. any way to get around this? |