love_bug Posted October 8, 2007 Share Posted October 8, 2007 Hi, I am a beginner in php.. well i am stuck in a situation. I want to limit image tag <img> to 1 or 2 only. suppose : <?php $string = "<img src=1> this is text <img src=2> <img src=3> here\'s some text <img src=4>"; echo limitImage($string); function limitImage { // Some function to do remove more than 1 img tags .. } ?> Output : <img src=1> this is text here\'s some text I hope you understood.. i think its simple.. but i have no idea how to do that.. thank you. Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/ Share on other sites More sharing options...
love_bug Posted October 8, 2007 Author Share Posted October 8, 2007 hello??? Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-364709 Share on other sites More sharing options...
kenrbnsn Posted October 8, 2007 Share Posted October 8, 2007 Please don't bump so quickly. Give us time. Ken Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-364711 Share on other sites More sharing options...
love_bug Posted October 9, 2007 Author Share Posted October 9, 2007 hello anyone?... :-\ Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-365133 Share on other sites More sharing options...
darkfreaks Posted October 9, 2007 Share Posted October 9, 2007 <?php ///if return more than one image tag error if (strpos($string,'<img src="">',>1)) { echo " You can only Upload one Image"; }else{ insert into DB; } ?> Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-365142 Share on other sites More sharing options...
MasterACE14 Posted October 9, 2007 Share Posted October 9, 2007 and if you're putting it into a function, put the function before it is actually called Regards ACE Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-365149 Share on other sites More sharing options...
love_bug Posted October 9, 2007 Author Share Posted October 9, 2007 Hi thanks for the solution but... I want to find all image tags in the string, keep the first image (img tag) and remove all others. basically to show just one image..if string contains 2 or more images.. Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-365173 Share on other sites More sharing options...
darkfreaks Posted October 9, 2007 Share Posted October 9, 2007 <?php if (strpos($string,'<img src="">',>1)) { $string= str_replace('<img src="">','',$string);} }else{ insert into DB; } ?> Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-365176 Share on other sites More sharing options...
darkfreaks Posted October 9, 2007 Share Posted October 9, 2007 could use something like <?php function str_replace_every_other($needle, $replace, $haystack, &$count=null, $replace_first=true) { $count = 0; $offset = strpos($haystack, $needle); //If we don't replace the first, go ahead and skip it if (!$replace_first) { $offset += strlen($needle); $offset = strpos($haystack, $needle, $offset); } while ($offset !== false) { $haystack = substr_replace($haystack, $replace, $offset, strlen($needle)); $count++; $offset += strlen($replace); $offset = strpos($haystack, $needle, $offset); if ($offset !== false) { $offset += strlen($needle); $offset = strpos($haystack, $needle, $offset); } } return $haystack; } //Use it like this: $str = "one two one two one two"; echo str_replace_every_other('one', 'two', $str, $count).'<br />'; //two two one two two two echo str_replace_every_other('one', 'two', $str, $count, false).'<br />'; //one two two two one two ?> Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-365186 Share on other sites More sharing options...
love_bug Posted October 9, 2007 Author Share Posted October 9, 2007 Thanks.. wish i could write the code.. the code seem to be working.. but it can not find image tags with different values.. can you show me how can i use it with perg_replace? <?php $URLSearchString = "a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'"; $str = preg_replace("(\<img src=([$URLSearchString]*)\](.+?)\</a>)", '$2', $str ); ?> Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-365206 Share on other sites More sharing options...
darkfreaks Posted October 9, 2007 Share Posted October 9, 2007 what do u mean? Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-365209 Share on other sites More sharing options...
love_bug Posted October 9, 2007 Author Share Posted October 9, 2007 the goal is to keep first <img tag and remove all other <img tags..... ??? Link to comment https://forums.phpfreaks.com/topic/72328-how-limit-html-tags-in-php/#findComment-365222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.