strip html tag by replacing comma at the end of each tag

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfaisalwarraich
    New Member
    • Oct 2007
    • 194

    #1

    strip html tag by replacing comma at the end of each tag

    Hi everybody,

    i have some data which i copied from a web page. its all option values of a select box. now im trying to find a way where i can add a coma at the end of each </option> tag.

    here is my code which i took from php.net:
    Code:
    $text = '<option value="test 1">test 1</option><option value="test 2">test 2</option>';
    	    $text = preg_replace('/</',' <',$text);
    	    $text = preg_replace('/>,/','> ',$text);
    		$desc = html_entity_decode(strip_tags($text));
    		$desc = preg_replace('/[\n\r\t]/',' ',$desc);
    		$desc = preg_replace('/  /',' ',$desc);
    		echo preg_replace('/[\ ]/', ',', $desc);
    this is replacing each empty space with a coma not at the end of tag.
    please anybody can help me to find out a way where i can do it?
    Last edited by Dormilich; Jul 14 '10, 02:16 PM.
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    You don't need all that. You can just use str_replace.
    This will add comma on the end of each </option> tag:

    Code:
    echo str_replace("</option>", "</option>,", $text);
    But what I don't understand is what do you need something like that for?

    Comment

    • mfaisalwarraich
      New Member
      • Oct 2007
      • 194

      #3
      it worked thanx. well i need it to have a coma separated values and then im gona explode that values and then insert them into database using foreach loop. its adding a coma at the end of text too but i think i can check for a value if its empty before inserting into the database.

      Comment

      • zorgi
        Recognized Expert Contributor
        • Mar 2008
        • 431

        #4
        Oups sorry you can just add string rtrim ( string $str [, string $charlist ] ) to fix the problem of the last comma:

        Code:
        echo rtrim(str_replace("</option>", "</option>,", $text), ",");

        Comment

        • mfaisalwarraich
          New Member
          • Oct 2007
          • 194

          #5
          thank you again for ur help.

          Comment

          Working...