crnaovca Posted September 16, 2010 Share Posted September 16, 2010 For a year or so I have a big problem I have to solve, I HAVE to solve, but still don't know how. Problem at first was in a approach to the problem, and now it is in regex part. Since I am not good at regex at all, and I actually need it only for this one problem, please help me to solve this. In CMS I have WYSWYG editor that gives me source of text like this <p> Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text <img alt="" src="http://www.site.com/v3/pics/Tuning_1.jpg" style="width: 400px; height: 300px;" /></p> <p> Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text <img alt="" src="http://www.site.com/v3/pics/Tuning_2.jpg" style="width: 400px; height: 300px;" /> Some text Some text Some text Some text Some text Some text Some text Some text problem is that I have to change src part to add phpthumb.php in front (full url to phpthumb, not only file name) and in the end of src I have to put "width=400" or similar, 99% width AND must remove style and alt tag ... So solution I have come up is that I have to find somehow all src URLs and then to remove all IMG tags and find replace them with new img tags I create as I want to. Anyone have better idea? Or idea on how to do this I imagined? problem in the end is in whywyg editor that gives me something I cannot change easily. I thing regex would be great to do this and most elegant way on doing it. Problem could be in that I am not sure if img tag look like this <img alt="" src="http://www.site.com/v3/pics/Tuning_2.jpg" style="width: 400px; height: 300px;" /> or <img src="http://www.site.com/v3/pics/Tuning_2.jpg" alt="" style="width: 400px; height: 300px;" /> or <img alt="" style="width: 400px; height: 300px;" src="http://www.site.com/v3/pics/Tuning_2.jpg"/> or somehow else ... ------- to short this up ... how to find all URLs and find replace all img tags with new tags I create as I want to ... (similar topic was here http://www.phpfreaks.com/forums/index.php?topic=229756.0 but since I do not know regex, I don't know how to change this) Quote Link to comment https://forums.phpfreaks.com/topic/213601-hello-everybody-one-year-old-problem/ Share on other sites More sharing options...
Psycho Posted September 17, 2010 Share Posted September 17, 2010 OK, I read this post twice and then took a look at the previous post. In the previous post the very first response stated: I'm not sure I follow.. you haven't provided a before AND after sample...(no offense..most people do not know how to accurately describe what they are looking for.. that is why it is always advisable to show a sample of what you start with [which you did], and what you want it to be afterwards.. Looking at the post above, where exactly do you show the after of what you want? I have no idea how to interpret this statement: problem is that I have to change src part to add phpthumb.php in front (full url to phpthumb, not only file name) and in the end of src I have to put "width=400" or similar, 99% width AND must remove style and alt tag ... Quote Link to comment https://forums.phpfreaks.com/topic/213601-hello-everybody-one-year-old-problem/#findComment-1112098 Share on other sites More sharing options...
Psycho Posted September 17, 2010 Share Posted September 17, 2010 The following will replace the src attribute in all image tags with "http://www.mysite.com/phpthumb.php". I think you actually want more than that, but you're going to have to do a better job of explaining/showing examples if you need more. $output = preg_replace("#(<img [^>]+src=\")([^\"]+)#", '${1}http://www.mysite.com/phpthumb.php', $input); Quote Link to comment https://forums.phpfreaks.com/topic/213601-hello-everybody-one-year-old-problem/#findComment-1112112 Share on other sites More sharing options...
Psycho Posted September 17, 2010 Share Posted September 17, 2010 //Replace src attribute $text = preg_replace("#(<img[^>]+src=[\"|'])([^\"|']+)#", '${1}http://www.mysite.com/phpthumb.php', $text); //Remove style attribute (if exist) $text = preg_replace("#(<img[^>]+?)(style=[\"|'][^\"|']+[\"|'])#", '${1}', $text); //Add custom style attribute $text = preg_replace("#(<img )([^>]+)#", '${1}style="width:400px;" ${2}', $text); Quote Link to comment https://forums.phpfreaks.com/topic/213601-hello-everybody-one-year-old-problem/#findComment-1112130 Share on other sites More sharing options...
crnaovca Posted September 17, 2010 Author Share Posted September 17, 2010 Maybe problem is that English is not my native language so syntax is wrong. Here is before example: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. <img alt="something" src="http://www.somesite.com/images/image1.jpg" style="width: 400px; height: 300px;" /> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco <img alt="something" src="http://www.somesite.com/images/image2.jpg" style="width: 400px; height: 300px;" /> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco And here is what I want to get: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. <img alt="name of site insted of default" src="http://www.site.com/phpthumb.php?http://www.somesite.com/images/image1.jpg?w=400" /> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco <img alt="name of site insted of default" src="http://www.site.com/phpthumb.php?http://www.somesite.com/images/image2.jpg?w=400" /> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco And I want it on all IMG tags in text so all pictures will be same width with doesn't matter wht user uploaded. Quote Link to comment https://forums.phpfreaks.com/topic/213601-hello-everybody-one-year-old-problem/#findComment-1112194 Share on other sites More sharing options...
Psycho Posted September 17, 2010 Share Posted September 17, 2010 The problem was you didn't give an example of what you wanted the output to be. The above explains the changes for src. But, what is this supposed to mean: alt="name of site insted of default" Where does the name of the site come from??? Also, you stated you wanted the images to have a width attribute. But in your example above you ahve removed the style attributes and appended (incorrectly) a "?w=400". So, is the 400 supposed to be appended to the URL (correctly with an ampersand) or do you need the attribute set in the img tag? The code below will replace the source as specified above (without the w=400). //Replace src attribute$text = preg_replace("#(<img[^>]+src=[\"|'])([^\"|']+)#", '${1}http://www.mysite.com/phpthumb.php?${2}', $text);//Remove style attribute (if exist)$text = preg_replace("#(<img[^>]+?)(style=[\"|'][^\"|']+[\"|'])#", '${1}', $text);//Add custom style attribute$text = preg_replace("#(<img )([^>]+)#", '${1}style="width:400px;" ${2}', $text); Quote Link to comment https://forums.phpfreaks.com/topic/213601-hello-everybody-one-year-old-problem/#findComment-1112202 Share on other sites More sharing options...
sasa Posted September 17, 2010 Share Posted September 17, 2010 <?php$test = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.<img alt="something" src="http://www.somesite.com/images/image1.jpg" style="width: 400px; height: 300px;" />Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco<img alt="something" src="http://www.somesite.com/images/image2.jpg" style="width: 400px; height: 300px;" />Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco';$out = preg_replace('~<img [^>]*src="(http://([^"/]+)[^"]+)"[^>]*>~', '<img alt="$2" src="http://www.site.com/phpthumb.php?$1?w=400" />', $test);echo $out;?> Quote Link to comment https://forums.phpfreaks.com/topic/213601-hello-everybody-one-year-old-problem/#findComment-1112215 Share on other sites More sharing options...
crnaovca Posted September 17, 2010 Author Share Posted September 17, 2010 i tried this last code above and nothing happened ?? when I go to view image link, I get normal link, not modified src (i changed test with my text) this ?w=400 is something that must be append to URL and I have to strip style part of tag, because I want phpthumb to modify picture width, and not to do it through style tag ... Quote Link to comment https://forums.phpfreaks.com/topic/213601-hello-everybody-one-year-old-problem/#findComment-1112244 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.