PHP - Replace First Occurrence
Pretty basic question here,
I have a string called: $string I want to use a php replace function or whatever would be best to find the first occurence of ] and for the string to delete from ] And everything after that first occurence of ] In the string. Example $string = "Hello Friends [Test] My name is John"; After the php function $string "Hello Friends [Test"; Similar TutorialsI want to identify strings with AT&T in them. Can't seem to find the right way to do this due to the &. I am using the strpos function in this manner: if (strpos(($string), 'AT&T')) > 0) I don't need to input or display using http. This is just a php script that I'll run from the command prompt. Thanks. I have a DB of data: article-id, section-id, subsection-id, article-text. When a row is fetched I am displaying article, section, subsection as <h3>, <h4>, <h5> and then text in a <p>. Before outputting the text I want to scan it for other articles, sections, or subsections it may reference then turn that reference into a hyperlink. I read the PHP documentation but I can't find anything that translates to the basic commands of instring, indexof, and left/mid/right. Example: 1. Article Name 100. Article 1, section 1 100.a article 1, section 1, subsection a Text blah blah blah 105.f blah blah blah
I want to find that 105.f and turn it into a link. All my articles, sections, and subsections are in the DB so I know I can use an array of the number values as my items to look for, but I can't figure out how to look. Is it pasible to find all occurrences of strings "North" and "North-East" in some txt file and save number of occurrences in some variables?? I wish to order my results by its occurrence inside the $status array. Basically all of the results with the status 'New' are to be listed first, then the results with the status Today, Tomorrow, Soon and so on. Is this possible, or is there alternatively a different approach to ordering my results how I desire? Code: [Select] $status = array('New', 'Today', 'Tomorrow', 'Soon', 'Future', 'Complete'); $display->get_results("SELECT * FROM todo ORDER by status DESC"); The situation is that I have a large string with some identifiers to say "replace me" with something else. My goal is to replace the values, but if one replacement includes one of the other keys, I don't want to replace it.
For example:
<?php $str = 'some blob ~abc~ followed by ~def~'; $arr_orig = array( '~abc~', '~def~' ); $arr_new = array( 'value~def~', 'blah' ); echo str_replace( $arr_orig, $arr_new, $str ); ?>This will echo: some blob valueblah followed by blah But that's not what I'm trying to accomplish. Basically, I want ~abc~ to be replaced with the literal: value~def~ and for the ~def~ that happens to be part of that string to NOT be replaced. I hope what I'm asking is clear. Basically, preg_replace/str_replace with arrays seems no different than just doing a FOR loop and replacing ~abc~ and then ~def~. But that's not what I need. I'm hoping that if ~abc~ is replaced with text that happens to be another identifier -- ~def~, ~xyz~, whatever -- that this already replaced text is not again replaced. Is there a built-in function to do something like this? If not, should I just parse the whole string and count characters or something? Seems like a pain (and slow!) Hi i wrote a find and replace code but my code replace last value of array and skip the first, second.... values in the array. Please give me an idea because i stack with this code. Regards $KeyWord = explode("\n", $RowGetWords['KEYWORDS']); $Replace = explode("\n", $RowGetWords['ReplaceTo']); for($i=0; $i<count($KeyWord); $i++){ $pattern = $KeyWord[$i]; $replace = "<a href=\"" .$URL. "\" target=\"_blank\" >" .$Replace[$i]. "</a>"; $html = str_replace($pattern, $replace, $Row['MessageBody']); } ok so I have an string with data that I need to be replaced with contents from an array. I need the occurance position replace with the index of the array. example Code: [Select] //here is the setup $string = "testing %s one %d %s"; array('this',1,'three'); //here is the result testing this one 2 three the problem I am having is because the %s can occur more than once I cant use str_replace. I am working on a solution with substr_replace but was wondering if anyone had any suggestions on how to do this. When I replace it I also need to run the value through a function so keep that in mind. Also the replace's are stored in an array so they need to be looped through as well. Code: [Select] $replaces = array('%s'=>'func_one','%d'=>'func_two'); Any help is appreciated. I have multiple \n in a string, and I need to replace it with <br />. How do I go about doing this? Preg replace? How does wordpress and other similar CMS/applications replace their div elements? For example, if you view the source code of a wordpress site, you will only see: Quote <div id="header"></div> Where, the above code is clearing being switched out for "header.php". How is this done? I believe it's php and jquery, but I would be wrong. Thanks! Hello, I'm trying to convert a paragraph of text which is already html formatted. I wish to find keywords, such as, 'PHP', which are contained in the paragraph, and replace them with a link, for example <a href="http:/www...">PHP</a> str_replace('PHP', '<A href=""....">PHP</a>', $content); My problem arises when some of these keywords are already contained in a link, so say, visit PHP website, is all one link, so str_replace would simply convert the PHP in this string, to two links for one single word. Code: [Select] <a href="">visit <A href=""....">PHP</a> website</a>How could this problem be resolved? Thanks I retrieve urls that have the following path $url=http://cdn.test1.com $url=http://cdn.test2.com and so on... i want to to do a string replace such that it displays http://test.com,http://test2.com and son without the cdn part. Will the following work for all cases? Code: [Select] $url=str_replace("cdn.","",$url); alright, is this the correct format for a REPLACE INTO? if not what do i need to change.......it isn't working...... Code: [Select] $sql = "REPLACE INTO Events (name, event, eventid, userid, title1, title2, title3, title4, title5, title6, title7, title8, email) VALUES('$name','$event','$eventid','$userid','$title1','$title2','$title3','$title4','$title5','$title6','$title7','$title8', '$email')"; $rs = mysql_query($sql) or die ("Problem with the query: $sql<br>" . mysql_error()); Hi, I'm pretty new to PHP, and I'm trying to write my own encoding/decoding script (not with base_64). I'd like to replace like all A's with B's, all B's with C's, C's with D's, and so on. How would I do this? Here's an example: String #1: "ABCDEFG" string #1 goes through my encryption string #2 (output): "BCDEFGH". How would I go about doing this? Thanks. Hello: I have a quick question. I am currently using the following code to replace an apostrophe and quote marks when inserting and updating a database record: Code: [Select] $myPageContent = mysql_real_escape_string(str_replace("'", "'", $_POST['myPageContent'])); $myPageContent = mysql_real_escape_string(str_replace("", """, $_POST['myPageContent'])); My question is - how can I modify it so I only need to use one line. Like (but this does not work): Code: [Select] $myPageContent = mysql_real_escape_string(str_replace("'", "'", "", """, $_POST['myPageContent'])); It seems to me that the way I am currently doing it might be POSTing the data twice? Thanks! User can edit a post. I need to replace the html '<br/>' element to newline character ('\n') . When a user will edit a post he/she will not see the '<br/>'. How can i do this with php ? $name = preg_replace('{(\d+)}', convertNumber("$1"), $row['name']); why isnt this working? Hello everyone, I'm attempting to find only a specific username in this field "username" located in the database. The code below works when with the rest of the I left out since its not important for this problem. What I would like to do is have it so if I type in the name "john, emily, or chris", and if the name is in the field, then it works. Code: [Select] session_register("username"); // Working code, username is the field name. Ex: Code: [Select] session_register("john | emily | chris"); // Example code, populated with values. I would like to work the same, except with values instead of using the field name. Let me know if you need any further clarification, thank you. Hello, I need a php function or some type of little parser that will find if a user submit's a Code: [Select] ' then it converts it into Code: [Select] "before I grab it and submit it into my database? How would I go about getting some code to convert all the input ' to " ? Thank you, The output of the array1 is looks as below. Code: [Select] Array ( [num_found] => 60 [articles] => Array ( [0] => Array ( [aut] => Array ( [0] => ANI ) [by] => ANI [Id] => 278 [key] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [norm] => true [body] => Gorging [cat] => Array ( [0] => food & drink ) [pm] => ANI|P#M#D| [pm] => Beware! [seo] => junk [pmT] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => Health [sd] => 3506926 [ti] => Beware! Junk food can cause miscarriage [ur] => riage [kick] => [subtitle] => [image] => [Tag] => ) [1] => Array ( [aut] => Array ( [0] => URVASHI ASHER ) [by] => URVASHI [Id] => 277 [key] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [norm] => true [body] => division. [cat] => Array ( [0] => entertainment ) [pm] => URVASHI ASHER|P#M#D|TNN [pm] => booked [seo] => film [pmT] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => News & Interviews [sd] => 3921796 [ti] => Amitabh booked [uri] => film [kick] => [subtitle] => [image] => [Tag] => ) I have a array2 similar to this. Code: [Select] Array ( [num_found] => 70 [articles] => Array ( [0] => Array ( [aut] => Array ( [0] => ANI ) [by] => RNI [Id] => 643 [key] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hy ) [norm] => true [body] => Test [cat] => Array ( [0] => drink ) [pm] => ANI|P#M#D| [pm] => Beware! [seo] => junk [pmT] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [pid] => 73 [Date] => 2011-06-02T00:00:00Z [section] => Health [sd] => 8434 [ti] => Beware [ur] => riage [kick] => [subtitle] => [image] => [Tag] => ) [1] => Array ( [aut] => Array ( [0] => URVASHI ASHER ) [by] => URVASHI [Id] => 345 [key] => Array ( [0] => film [1] => design [2] => biography [3] => bachchan [4] => cinema ) [norm] => true [body] => division. [cat] => Array ( [0] => entertainment ) [pm] => URVASHI|P#M#D|TNN [pm] => booked [seo] => film [pmT] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => News & Interviews [sd] => 43543543 [ti] => booked [uri] => film [kick] => [subtitle] => [image] => [Tag] => ) Please tell me how to add the 1st value from array2 and replace in array1. The output should look like Code: [Select] Array ( [num_found] => 60 [articles] => Array ( [0] => Array ( [aut] => Array ( [0] => ANI ) [by] => RNI [Id] => 643 [key] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hy ) [norm] => true [body] => Test [cat] => Array ( [0] => drink ) [pm] => ANI|P#M#D| [pm] => Beware! [seo] => junk [pmT] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [pid] => 73 [Date] => 2011-06-02T00:00:00Z [section] => Health [sd] => 8434 [ti] => Beware [ur] => riage [kick] => [subtitle] => [image] => [Tag] => ) [1] => Array ( [aut] => Array ( [0] => URVASHI ASHER ) [by] => URVASHI [Id] => 277 [key] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [norm] => true [body] => division. [cat] => Array ( [0] => entertainment ) [pm] => URVASHI ASHER|P#M#D|TNN [pm] => booked [seo] => film [pmT] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => News & Interviews [sd] => 3921796 [ti] => Amitabh booked [uri] => film [kick] => [subtitle] => [image] => [Tag] => ) where array[0] is the value from array2. User can post anything. After a line user can hit enter and can start a new sentence. I wan to replace the newline character ('\n') to html '<br/>' element. I need to output a break in html page. How can i do this with php ? How can i check whether a string contains '\n' or not ? Which function can i use for that ? |