aliento Posted May 28, 2009 Share Posted May 28, 2009 I have html source into a string i want the function which will replace the src="lalala.htm" to src="http://funny.com/lalala.htm" If you cant provide the code just guide me, Thank you Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/ Share on other sites More sharing options...
lonewolf217 Posted May 28, 2009 Share Posted May 28, 2009 going literally by what you say, this will work $string = "src=\"lalala.htm\""; $string = str_replace("src=\"lalala.htm\"","src=\"http://funny.com/lalala.htm\"",$string); of course, you probably meant that you want ANYTHING after src="...." to be converted into a proper link, and this would require REGEX, so you probably want to ask in the forum above called PHP Regex Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844105 Share on other sites More sharing options...
aliento Posted May 28, 2009 Author Share Posted May 28, 2009 ok i will post there! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844108 Share on other sites More sharing options...
Daniel0 Posted May 28, 2009 Share Posted May 28, 2009 ok i will post there! Thank you No. Double posting is in fact against the rules. I can move this topic for you if you wish. Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844110 Share on other sites More sharing options...
aliento Posted May 28, 2009 Author Share Posted May 28, 2009 yes please Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844111 Share on other sites More sharing options...
aliento Posted May 28, 2009 Author Share Posted May 28, 2009 ok i will post there! Thank you No. Double posting is in fact against the rules. I can move this topic for you if you wish. Could you please delete this post? I want to make a new one more detailed. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844146 Share on other sites More sharing options...
Daniel0 Posted May 28, 2009 Share Posted May 28, 2009 What prevents you from posting the details here? Generally we do not delete anything that doesn't violate our terms of service. Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844156 Share on other sites More sharing options...
aliento Posted May 28, 2009 Author Share Posted May 28, 2009 Great, i have read how to post a question in Regex forum so : What i want is to load the page from another directory than pages directory this needs the src's to be src="http://domain.com/page.htm" and not src="page.htm" . I have a string with an html page in it and a string with the sites domain. So i want to change every src into the page that it doesnt include domain with the http://domain+page. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844164 Share on other sites More sharing options...
Adam Posted May 28, 2009 Share Posted May 28, 2009 Try this... <?php $content = 'src="lalala.html" ..efwef943434knrg src="http://lelelele.html" werfer4m34/rg5tkrwe3 src="lululul.html"'; $domain = 'http://example.com/'; $content = preg_replace('/src="(?!http:\/\/)([^"]+)"/', 'src="' . $domain . '$1"', $content); print $content; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844186 Share on other sites More sharing options...
Adam Posted May 28, 2009 Share Posted May 28, 2009 Just thought... I don't know what you plan on doing when the path has a leading slash, but to prevent them from having two slashes when you add 'http://example.com/' to the front of it, change the regex slightly to: src="\/?(...) - if you get me? Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844192 Share on other sites More sharing options...
aliento Posted May 28, 2009 Author Share Posted May 28, 2009 MrAdam thank you, and thank you for the correction , now its perfect. I always called regex as php magic, today i learned that is called regex! I am very interesting to learn it and i am very curious of it. It seems to me that is a program language inside php. Do you have in mind any php book bible that has even regex in it? Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844204 Share on other sites More sharing options...
nrg_alpha Posted May 28, 2009 Share Posted May 28, 2009 Do you have in mind any php book bible that has even regex in it? The book I always recommend with regards to learning regex is Mastering Regular Expressions. Additional online resources include: regular-expressions weblogtoolscollection php freaks regex tutorial php freaks resources More than enough to get you started Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844240 Share on other sites More sharing options...
aliento Posted May 28, 2009 Author Share Posted May 28, 2009 thnx! Quote Link to comment https://forums.phpfreaks.com/topic/160016-find-n-replace-function/#findComment-844245 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.