johnmerlino Posted May 31, 2011 Share Posted May 31, 2011 Hey all, I have method like this: echo tinymce_tag($post->body); Basically the body property contains a string from database containing one or many paragraphs delimited by p tags: <p>Hello World.</p> <p>Goodbye World.</p> I want to give all the paragraphs a class. This function will give the first p tag a class: if ( ! function_exists('tinymce_tag')){ function tinymce_tag($content = ''){ $pos = strpos($content, '<p>'); if($pos !== false){ $content = substr_replace($content,' class="paragraph_styler"', 2, 0); } return $content; } } But I am not sure how to give all the p tags in the content variable a class. Thanks for response. Quote Link to comment https://forums.phpfreaks.com/topic/238046-iterate-through-a-string-looking-for-all-occurrences-of-a-substring/ Share on other sites More sharing options...
xyph Posted June 1, 2011 Share Posted June 1, 2011 Why not use str_replace( '<p>', '<p class="style">', $string ); Quote Link to comment https://forums.phpfreaks.com/topic/238046-iterate-through-a-string-looking-for-all-occurrences-of-a-substring/#findComment-1223256 Share on other sites More sharing options...
johnmerlino Posted June 4, 2011 Author Share Posted June 4, 2011 thanks Quote Link to comment https://forums.phpfreaks.com/topic/238046-iterate-through-a-string-looking-for-all-occurrences-of-a-substring/#findComment-1225083 Share on other sites More sharing options...
johnmerlino Posted June 5, 2011 Author Share Posted June 5, 2011 can I use a regular expression with str_replace if, for example, I want to replace a number of different characters like "_" "-"? Quote Link to comment https://forums.phpfreaks.com/topic/238046-iterate-through-a-string-looking-for-all-occurrences-of-a-substring/#findComment-1225286 Share on other sites More sharing options...
xyph Posted June 5, 2011 Share Posted June 5, 2011 No, you want to use preg_replace() Quote Link to comment https://forums.phpfreaks.com/topic/238046-iterate-through-a-string-looking-for-all-occurrences-of-a-substring/#findComment-1225578 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.