PHP - Str Replace And Re Write
how can i find reference to something like this "navigationID=3" or "navigationID=44" within a string and then re write it to become something like this "/en/content/3/Link_Name"?
there could be more than one reference within the string and the integer val (ie 3 and or 44) will not be known. Any help with this would be greatly appreciated. Similar TutorialsThe 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']); } 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! 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. 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! 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 ? 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 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()); I want to create a xml-based rss which is readable by RSS readers Code: [Select] <?xml version="1.0"?> <rss version="2.0"> <channel> <title>Title</title> <link>http://link.com/</link> How I can write php strings from a php file into it as Code: [Select] <item> <title><? echo $title ?></title> <link><? echo $link ?></link> <description><? echo $description ?></description> <pubDate><? echo $date ?></pubDate> <guid><? echo $id?></guid> </item> Hello dear friends, let say we have text file (file.txt) i wanna php code that makes me able to write some text to this text file (file.txt) then save it as zipped file to be (zipped.zip) 1) I know how to write , will use this Code: [Select] <?php $File = "file.txt"; $Handle = fopen($File, 'w'); $Data = "Hello phpfreaks\n"; fwrite($Handle, $Data); $Data = "Hello World\n"; fwrite($Handle, $Data); print "Data Written"; fclose($Handle); ?> now the (file.text) will be saved with the following text Code: [Select] Hello phpfreaks Hello World Now the question which i do not know how to save it as zipped file not text file ? OR save it normally as text file (file.txt) then create zipped file of it (zipped.zip) thank you so much for help hello how would i write, if $a has 1 or more of $b do something ? for example Code: [Select] if($a >1 $b){ do something }else{ do nothing } or >2 or >3 etc... thanks Code: [Select] if (file_exists("images/".$id."/img01.jpg")) { echo "<a href=\"/v1/images/".$id."/img01.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb01.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; } if (file_exists("images/".$id."/img02.jpg")) { echo "<a href=\"/v1/images/".$id."/img02.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb02.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; } if (file_exists("images/".$id."/img03.jpg")) { echo "<a href=\"/v1/images/".$id."/img03.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb03.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; } if (file_exists("images/".$id."/img04.jpg")) { echo "<a href=\"/v1/images/".$id."/img04.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb04.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; } if (file_exists("images/".$id."/img05.jpg")) { echo "<a href=\"/v1/images/".$id."/img05.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb05.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; } if (file_exists("images/".$id."/img06.jpg")) { echo "<a href=\"/v1/images/".$id."/img06.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb06.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; } if (file_exists("images/".$id."/img07.jpg")) { echo "<a href=\"/v1/images/".$id."/img07.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb07.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; } if (file_exists("images/".$id."/img08.jpg")) { echo "<a href=\"/v1/images/".$id."/img08.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb08.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; } if (file_exists("images/".$id."/img09.jpg")) { echo "<a href=\"/v1/images/".$id."/img09.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb09.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a>"; } This code works but it just seems a bit tedious. I may also include up to 20 images to check if they exist. Better way to do this? Hello, how could I do a replace using one array? Example: Code: [Select] $changes = array( "{NAME}" => $name, "{CHANGED_PART}" => $desc, "{CHANGE_D}" => date("j"), "{CHANGE_M}" => date("n") ); And then I would want to replace all parts in the left with parts in the right inside a string.Is that possible ? thank you.. 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. 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 long string of html, and what I want to do, if find all occurrences of: /static (anything after)... and replace with: http://www.mydomain.com/static (anything after). What is the best and fastest way to do this? Thanks. 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. |