PHP - Replace Problem
Hey guys i am trying to search all href in a string and add someting in front so i can catch some stats before i redirect the user.
For exemple : href="http://twitter.com" Should be changed into: http://test.ca?dat='.urlencode("http://twitter.com").' But i can't get it to work ... so what i got so fare is <?php $str = "<a target='_blank' href='http://twitter.com'>TEST 1</a> <a target='_blank' href='http://twitter.com'>TEST 2</a>"; $str .= ' <a href="http://twitter.com">TEST 1</a> <a href="http://twitter.com">TEST 2</a>'; function matche($matches){ $url = str_replace('http://', '', $matches[1]); $url = str_replace('www.', '', $url); return 'href="http://test.ca?email=XX_EMAIL&nid=XX_ID&dat='.urlencode($url).'"'; } echo preg_replace_callback("/href=['\"]([.*]+)['\"]/","matche",$str); ?> Similar TutorialsHi, I am having a pretty difficult time getting files to upload to my server that have a Single Quote in the file name. Example: myfile's.pdf I need to strip the file name of the quote before it uploads to the server. I currently am using SWFUpload and this is the bit of code that was included with it. I need to modify it to remove the single quotes as well. Been hammering my head over this all day. $file_name = preg_replace('/[^'.$valid_chars_regex.']|\.+$/i', "", basename($_FILES[$upload_name]['name'])); if (strlen($file_name) == 0 || strlen($file_name) > $MAX_FILENAME_LENGTH) { HandleError("Invalid file name"); exit(0); } thanks I have a number of records of the html sections of a series of Google Earth placemarks that I managed to extract from the raw kml file. The purpose of the exersize was to then use the simplehtmldom.php api to extract the DATA from the raw html code. Some of the process is going well... and some is NOT. I have found that if I modify the raw html code by entering ID attributes into the html code the simplehtmldom api has an easy time identifying the desired data, and the data can be far 'cleaner' by entering an id attribute as 'close' to the data as possible. But doing a php text search and replace often requires finding a 'unique' identifyable portion of the html code and THEN placing the 'id' attribute in a nearby html tag because the desired data is nested inside a non-unique tag. As in I can identify a SPECIFIC <td> tag section where the data i desire is located but the data is nested inside a <font> tag inside the <td> cluster. Hence my problem... If I do a search in the following code... Code: [Select] <td><b><font size="+2" color="#FF0000">Neighborhood:</font> <font size="+2" color="#0000FF">City of Sidney</font></b></td> I can locate the 'Neighborhood:' string because it is unique in the whole html code. Then by some charcter counting I am desiring to put my 'id' attribute in the NEXT font tag because it surrounds the desired data the 'City of Sidney'... as in... Code: [Select] <td><b><font size="+2" color="#FF0000">Neighborhood:</font> <font id="neighborhood" size="+2" color="#0000FF">City of Sidney</font></b></td> With this modification the desired data is easily found and cleanly produced. But the html code while all operating correctly in a web page is not all identicle from a 'whitespace' point of view AND thus my problem. If I search the following code... Code: [Select] <td> <b><font size="+2" color="#FF0000">Neighboorhood:</font> <font size="+2" color="#0000FF">Greenacre</font></b> </td> While being identical as far as html is concerned if I search this code for the 'Neighboorhood:' identifier I find it... but then attempting to place the id tag into the NEXT font tag is being problematic. What i seem to need is a function that once the 'Neighboorhood:' string position is identied and noted in the whole of the html code, to FIND and modify the NEXT occurance of a font tag no matter what whitespace (or special charachters) may be occuring. Any suggestions?? eatc7402 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']); } I have multiple \n in a string, and I need to replace it with <br />. How do I go about doing this? Preg replace? 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 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! 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! 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()); 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 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); 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. 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 ? basically what i am trying to do is replace the <a href="whatever.com"> with <a href="whatever.com" onclick = "if (! confirm("Continue?")) return false;"> so that when the user clicks the url in a message the box pops up first then redirects them to the url if they click confirm. However i am getting the following error: Code: [Select] Warning: preg_replace() [function.preg-replace]: Compilation failed: reference to non-existent subpattern at offset 11 here is the code i used. $message_content = $content_info['message_content']; $pattern='<a href="\\1">'; $replace='<a href="\\1" onclick = "if (! confirm("Continue?")) return false;">'; $message_content = preg_replace($pattern, $replace, $message_content); echo $message_content; Anyone know where i went wrong? I have a site that allows members to create pages. These pages consist of small sections some of which HTML can be entered. To keep visitors from straying away from the site, any outside urls are brought up in a modal window. Using inline attributes, a class has to be created (or added to), and the title tag requires some special characters. I can probably get around this by inserting an onclick or something, but there must be a way to replace attributes, and I am at a loss. The text segments are stored in a database. What I want to do is change the <a> tag attributes as the page is created. So I have a segment of text "$TheText" and I want to change or replace the attribute values. For instance, If $TheText contains an 'a' tag that looks like this: Code: [Select] <a class="MyClass" title="Godzilla" href="http://www.godzillaslair.com">Godzilla</a>I would want to change it to Code: [Select] <a class="MyClass Lightview" title="Godzilla :: {Lightview's parameters}">Godzilla</a>Or even just replace the attributes entirely, so it would look like Code: [Select] <a class="Lightview" title="{Inserted Title} :: {Lightview's parameters}">Godzilla</a> In other words, just replacing the attribute values. Of course, I need to see if the attributes exist. I know how to do that and how to get their values. But all that tells me is that I have an 'a' tag in the text with those values for those attributes. I cannot, for example, then do a str_replace in the first example, because there may be another tag in the text with the "MyClass" class. I can iterate character by character between <a and </a> but there must be a better way. I'm no good at regex, so I haven't even attempted that. Any suggestions would certainly be appreciated. 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. 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"; 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. Hi there! Hoping someone can help me with this very specific issue I have a string that looks like this: Code: [Select] var1 var2 X var3 var4 X var5 I need to find the highest number next to var (in this case 5), and to replace X with the following number (in this case var6). So after the replace, the string would look like this: Code: [Select] var1 var2 var6 (this x has been replaced for var6) var3 var4 var7 (this x has been replaced for var7) var5 Any ideas!? Thanks! |