SchweppesAle Posted May 19, 2009 Share Posted May 19, 2009 if I had a very large string. Is there a way of implementing preg_replace in order to correct spaces within image names? preg_replace("\ \","%20",$string); works but I'm trying to only target characters within the img -> src tag attribute. thanks Quote Link to comment https://forums.phpfreaks.com/topic/158803-solved-spaces-in-image-names/ Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 Why use preg_replace on that? Why not use it when that src URL was generated? Quote Link to comment https://forums.phpfreaks.com/topic/158803-solved-spaces-in-image-names/#findComment-837567 Share on other sites More sharing options...
SchweppesAle Posted May 19, 2009 Author Share Posted May 19, 2009 the string itself is being pulled from our database. I need to modify it after the fact. Was kind of hoping that there's an easy to accomplishing this. Quote Link to comment https://forums.phpfreaks.com/topic/158803-solved-spaces-in-image-names/#findComment-837571 Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 So you stored the image in the DB as - '<img src="..." alt="..." />' instead of just the src URL? Quote Link to comment https://forums.phpfreaks.com/topic/158803-solved-spaces-in-image-names/#findComment-837580 Share on other sites More sharing options...
SchweppesAle Posted May 19, 2009 Author Share Posted May 19, 2009 actually, there are sometimes multiple images. The database itself is part of a Joomla 1.5 CMS. Usually, images are stored with the following format <img align="left" width="209" src="../images/stories/intuit logo new for 2008.jpg" height="90" /> They're in there with the actual content though, so I'm not really sure how to get in there and modify only the required src = "" string Quote Link to comment https://forums.phpfreaks.com/topic/158803-solved-spaces-in-image-names/#findComment-837592 Share on other sites More sharing options...
nrg_alpha Posted May 19, 2009 Share Posted May 19, 2009 Since the request was for regex, I moved the thread to the regex board. Please note this board for future regex requests. Database issues not withstanding, if going the regex route, is this something along the lines of what you are looking for? $str = 'img align="left" width="209" src="../images/stories/intuit logo new for 2008.jpg" height="90" />'; if(preg_match_all('#src=[\'"][^\'"]+[\'"]#i', $str, $matches)){ foreach($matches[0] as $val){ $str = str_replace($val, str_replace(' ',"%20",$val), $str); } } echo $str; // ouputs: img align="left" width="209" src="../images/stories/intuit%20logo%20new%20for%202008.jpg" height="90" /> Quote Link to comment https://forums.phpfreaks.com/topic/158803-solved-spaces-in-image-names/#findComment-837621 Share on other sites More sharing options...
SchweppesAle Posted May 19, 2009 Author Share Posted May 19, 2009 wow, thanks man. that's exactly right. Quote Link to comment https://forums.phpfreaks.com/topic/158803-solved-spaces-in-image-names/#findComment-837755 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.