l0rdz3r0 Posted October 28, 2009 Share Posted October 28, 2009 hello everyone i need some help in this i tried many ways i google it etc... and yes i am new in this... Here goes my headaches $output1='<a href="http://www4.site.com/photo/a/user/img/pict/name1212.jpg" rel="nofollow"> <img src="http://www4.site.com/photo/a/user/img/pict/name1212.jpg" border="0" alt="name" /></a>'; i need to change /img/ to /thumb/ at <img src... $links = str_replace('/img/', "/thumb/", $output1); this works but it changes the href too what i need is some help to the function only change in <img src somenting like this $links = str_replace('/img/ |<img src', "/thumb/", $output1); so the output will be <a href="http://www4.site.com/photo/a/user/img/pict/name1212.jpg" rel="nofollow"> <img src="http://www4.site.com/photo/a/user/thumb/pict/name1212.jpg" border="0" alt="name" /></a> Quote Link to comment https://forums.phpfreaks.com/topic/179377-solved-str_replace-url/ Share on other sites More sharing options...
mrMarcus Posted October 28, 2009 Share Posted October 28, 2009 excuse me if i'm over-simplifying this, but couldn't you just do this: $output1='<a href="http://www4.site.com/photo/a/user/img/pict/name1212.jpg" rel="nofollow"> <img src="http://www4.site.com/photo/a/user/thumb/pict/name1212.jpg" border="0" alt="name" /></a>'; why do you need to use str_replace() when you can just do it manually? perhaps a little more insight to the use of this might help shed some better responses. EDIT: if that won't do it for you, did you try this: $output1 = str_replace ('<img src="http://www4.site.com/photo/a/user/img/', '<img src="http://www4.site.com/photo/a/user/thumb/', $output1); pretty straight forward. Quote Link to comment https://forums.phpfreaks.com/topic/179377-solved-str_replace-url/#findComment-946460 Share on other sites More sharing options...
GingerRobot Posted October 28, 2009 Share Posted October 28, 2009 mrMarcus, i'm assuming that those are example values for various parts of the URL. To the OP: you'll need to use regular expressions. You can use the preg_replace function to do what you want. Quote Link to comment https://forums.phpfreaks.com/topic/179377-solved-str_replace-url/#findComment-946505 Share on other sites More sharing options...
mrMarcus Posted October 28, 2009 Share Posted October 28, 2009 mrMarcus, i'm assuming that those are example values for various parts of the URL.i'm not sure that's the case. all that is needed is when there is an <img> tag present, have the directory point to /thumb/ and the <a> tag point to /img/. now, call me crazy, but i would just hard-code it in if it's to be a constant. for example, assume a setup like this: <?php $sql = mysql_query ("SELECT * FROM `table`"); while ($res = mysql_fetch_assoc ($sql)) { $output1 = '<a href="http://www4.site.com/photo/a/user/img/pict/'.$img.'" rel="nofollow"><img src="http://www4.site.com/photo/a/user/thumb/pict/'.$img.'" border="0" alt="name" /></a>'; echo $output1; } ?> why the need to replace at all? code-in one dir for thumbs, one for full-size. i could be mistaken with how i interpreted the initial problem, and you're absolutely right, regex would take care of an issue like this .. but, is there really an issue? Quote Link to comment https://forums.phpfreaks.com/topic/179377-solved-str_replace-url/#findComment-946524 Share on other sites More sharing options...
l0rdz3r0 Posted October 28, 2009 Author Share Posted October 28, 2009 $output1 = str_replace ('<img src="http://www4.site.com/photo/a/user/img/', '<img src="http://www4.site.com/photo/a/user/thumb/', $output1); pretty straight forward. No i cant do that becouse a i get a list with multiple urls for ex. <a href="http://www4.site.com/photo/a/user/img/pict/name1212.jpg" rel="nofollow"> <img src="http://www4.site.com/photo/a/user/img/pict/name1212.jpg" border="0" alt="name" /></a> <a href="http://www8.site.com/photo/a/user/img/pict/na433212.jpg" rel="nofollow"> <img src="http://www8.site.com/photo/a/user/img/pict/na433212.jpg" border="0" alt="name" /></a> ... that why i need to get some that only change from img src... Quote Link to comment https://forums.phpfreaks.com/topic/179377-solved-str_replace-url/#findComment-946591 Share on other sites More sharing options...
mrMarcus Posted October 28, 2009 Share Posted October 28, 2009 where are these link being drawn from? an external source? from a db? do you have control over the path? Quote Link to comment https://forums.phpfreaks.com/topic/179377-solved-str_replace-url/#findComment-946613 Share on other sites More sharing options...
MadTechie Posted October 28, 2009 Share Posted October 28, 2009 $output1 = preg_replace('%src="(.*?)/img%i', 'src="\1/thumb', $output1); Quote Link to comment https://forums.phpfreaks.com/topic/179377-solved-str_replace-url/#findComment-946614 Share on other sites More sharing options...
l0rdz3r0 Posted October 28, 2009 Author Share Posted October 28, 2009 thanks MadTechie this is what i was looking nice work thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/179377-solved-str_replace-url/#findComment-946623 Share on other sites More sharing options...
MadTechie Posted October 28, 2009 Share Posted October 28, 2009 Welcome, If this is solved please click topic solved (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/179377-solved-str_replace-url/#findComment-946628 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.