Results 1 to 9 of 9
Jul 30, 2009, 03:27 #1
using preg_replace to replace a string
I've never used preg_repllace, so, m getting a bit confused to use them, i need to convert anyting like images/kid.jpg in my content to something like:
Here's how i get my content:
$filename = "template.php?id=$siteid¤tpage=$pagename";
Jul 30, 2009, 04:52 #2
The code that you posted has nothing to do with your request. Have you made any attempt to do the replacement on your own?
Jul 30, 2009, 04:58 #3
ya, i tried using preg_replace,but couldn't do it, so, didn't paste that code here, just pasted my original code
Jul 30, 2009, 05:12 #4
Show us what you tried, whether it worked or not, and we can advise from there. Or do you just want a solution handed to you on a plate?
Jul 30, 2009, 05:23 #5
well, since i have no knowledge about preg_replace, i just searched over google, and tried the code by copy/paste, but, couldn't get it, so, i decided to use str_replace, and it worked fine for me, only issue that i am facing is that i am not able to replace two search strings, i.e if the content contains either this or that or both, then replace both.
here's my code:
$Value = str_replace("$cmparray", "$root/workarea/sites/$siteid/images/", $sValue);
$sValue comes after reading from external file.
Hope its fine now
Jul 30, 2009, 05:43 #6
Try again without the double quotes around $cmparray.
Jul 30, 2009, 05:54 #7
Ah yea, my mistake...
but now i am facing the problem that i was actually afraid of, now if the content already has path like:
it gets replaced with:
Now i fear i have to use preg_replace for exact match.
Jul 30, 2009, 06:49 #8
Are you familiar with regular expressions at all? I fear just handing over a regular expression for you to use would wind the battle but not the war; I'd rather help teach you something than give copy/paste solutions.
If you have a spare few minutes, take a look at http://www.regular-expressions.info/ and particularly useful in your case will be anchors.
Jul 30, 2009, 23:25 #9
well, suppose my text is like:
there was a king who was <img src="images/king.jpg"> and he had a kindom <img src="http://www.abc.com/mysite/images/king.jpg">.
Now i want to replace images/king.jpg with http://www.abc.com/mysite/images/king.jpg
but my problem is that http://www.abc.com/mysite/images/king.jpg also get replace with http://www.abc.com/mysite/http://www...mages/king.jpg
I tried something like:
But, nothing works....