lily Posted December 5, 2007 Share Posted December 5, 2007 Hi, I need to find the images from some text. The images are coded within tags (I purposely put a space between img and ], so that you can see the tags). I have found a way to take off tag if there is no other text in string. For example: $RawBody="" $RawBody=preg_replace("#\[img\]([^?\['\"]+?)\.(gif|png|jpg|jpeg)\[/img\]#i", '\1.\2 ',$RawBody); I will get image name: myimage.jpg. But if there is other text in $RawBody, then I can not get right result. For example, if I change $RawBody to: $RawBody="This is my image , other text"; $RawBody=preg_replace("#\[img\]([^?\['\"]+?)\.(gif|png|jpg|jpeg)\[/img\]#i", '\1.\2 ',$RawBody); I will not be able to get result "myimage.jpg". What to do to get right image name in second case? Thanks for help! Link to comment https://forums.phpfreaks.com/topic/80300-solved-how-to-remove-img-and-img-tag/ Share on other sites More sharing options...
fanfavorite Posted December 5, 2007 Share Posted December 5, 2007 Couldn't you just use str_replace? You are just trying to remove the two tags and leave everything between? $RawBody = str_replace("[img ]","",$RawBody); $RawBody = str_replace("[/img ]","",$RawBody); Link to comment https://forums.phpfreaks.com/topic/80300-solved-how-to-remove-img-and-img-tag/#findComment-407022 Share on other sites More sharing options...
lily Posted December 5, 2007 Author Share Posted December 5, 2007 Thanks for reply, fanfavorite, But I need to remove all text befor too. if $RawBody="text, , other text"; I only need result "image.jpg", nothing else. Do you know what to do? Thanks again. Link to comment https://forums.phpfreaks.com/topic/80300-solved-how-to-remove-img-and-img-tag/#findComment-407035 Share on other sites More sharing options...
fanfavorite Posted December 5, 2007 Share Posted December 5, 2007 Try this: $RawBody="text, , other text"; $ini = strpos($string,"[img ]"); $ini += strlen("[img ]"); $len = strpos($string,"[/img ]",$ini) - $ini; $newstring = substr($string,$ini,$len); echo $newstring; Link to comment https://forums.phpfreaks.com/topic/80300-solved-how-to-remove-img-and-img-tag/#findComment-407044 Share on other sites More sharing options...
lily Posted December 5, 2007 Author Share Posted December 5, 2007 It works!! Many thanks, fanfavorite. Link to comment https://forums.phpfreaks.com/topic/80300-solved-how-to-remove-img-and-img-tag/#findComment-407104 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.