Working with array results

Discussion in 'PHP' started by j_o, Aug 15, 2011.

  1. #1
    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.
     
    j_o, Aug 15, 2011 IP
  2. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #2
    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?
     
    j_o, Aug 15, 2011 IP
  3. MancuZ28

    MancuZ28 Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    21
    #3
    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?
     
    MancuZ28, Aug 15, 2011 IP
  4. MancuZ28

    MancuZ28 Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    21
    #4
    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.
     
    MancuZ28, Aug 15, 2011 IP
  5. Technoslab

    Technoslab Peon

    Messages:
    46
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    0
    #5
    Maybe try this:

    
    $content = str_replace(array('<h2>','</h2>'),array('[H2]','[/H2]'),$content);
    
    Code (markup):
    For case insensitive replacement, try str_ireplace();
     
    Technoslab, Aug 16, 2011 IP
    j_o likes this.
  6. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #6
    Thanks everyone I went with the str_ireplace() function and it worked like a charm.
     
    j_o, Aug 16, 2011 IP
  7. ModulesGarden

    ModulesGarden Active Member

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #7
    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?
     
    ModulesGarden, Aug 16, 2011 IP
  8. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #8
    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:
     
    j_o, Aug 16, 2011 IP