Hi everyone, I and trying to brush up on my php and I have been playing around with content management systems most recently. I have a form that displays the content from the database in the form and it allows the content to be changed. I want the form to display the text in a way that is simple to read instead of plain html which can get complicated for non-coders. My first test that I have setup is just using some of the most basic tags for example I want an <h2> tag to convert to [H2] when it is displayed in the text area. My initial thought was to do it like this: $SplitContent = explode(" ",$Content); $Total = count($SplitContent); $i=0; while($i<=$Total){ if($SplitContent[$i] == "<h2>"){ $SplitContent[$i] = "[H2]"; } if($SplitContent[$i] == "</h2>"){ $SplitContent[$i] = "[/H2]"; } if($SplitContent[$i] == "<h3>"){ $SplitContent[$i] = "[H3]"; } if($SplitContent[$i] == "</h3>"){ $SplitContent[$i] = "[/H3]"; } if($SplitContent[$i] == "<p>"){ $SplitContent[$i] = "[P]"; } if($SplitContent[$i] == "</p>"){ $SplitContent[$i] = "[/P]"; } if($SplitContent[$i] == "<center>"){ $SplitContent[$i] = "[CENTER]"; } if($SplitContent[$i] == "</center>"){ $SplitContent[$i] = "[/CENTER]"; } if($SplitContent[$i] == "<br/>"){ $SplitContent[$i] = "[/BR]"; } if($SplitContent[$i] == "<b>"){ $SplitContent[$i] = "[B]"; } if($SplitContent[$i] == "</b>"){ $SplitContent[$i] = "[/B]"; } $i++; } PHP: However this is not converting the tags like I expected it to. Can anyone see what I did wrong? I also attempted to do this through a foreach loop however when that didn't work I decided to try the above code. Thanks in advance for any help.
So after looking at my code more indepth I figured out that the problem was that my explode had '<h2>TEXT' instead of '<h2> TEXT' which was making the if statements fail. Does this mean that I should them explode my text based on the < > tags instead?
I might be mis understanding, but can't you just do a find and replace on the tags you want to change? Or better yet, if you want to change all tags, look for regex and do a preg_replace() on the tag patterns, maybe?
And by "find and replace" I of course mean str_replace...but I do think regular expressions and preg_replace are the way to go.
Maybe try this: $content = str_replace(array('<h2>','</h2>'),array('[H2]','[/H2]'),$content); Code (markup): For case insensitive replacement, try str_ireplace();
Good to hear that you ahve solved your problem. I am just wondering why you don't used foreach function as it is being used especially for arrays like this?
As I mentioned earlier my first attempt was to use a for each loop, however when that was not working and I was trying to debug myself I switched the loop with the hope that it might solve the problem because I did the foreach wrong. In the end I did not end up using a loop at all instead I just used the following code: $Replace = array("<center>","</center>","<h2>","</h2>","<h3>","</h3>","<p>","</p>","<br/>","<b>","</b>"); $Search = array("[CENTER]","[/CENTER]","[H2]","[/H2]","[H3]","[/H3]","[P]","[/P]","[BR/]","[B]","[/B]"); $ChangedContent = str_ireplace($Search, $Replace, $Content); PHP: