replace text outside html syntax regexp php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andreas kirschner

    replace text outside html syntax regexp php

    hello regexp developers,

    how can i replace text outside a html syntax by using reg exps in php?

    I mean i have a text like
    <h3>heading</h3>
    <p>text</p>
    <table>
    <tr>
    <td>cell1</td>
    <td>cell2</td>
    </tr>
    </table>
    or any html code.

    now i want all the text outside the syntax (here: heading, text,
    cell1, cell2) replaced by a special code like <img...>.

    how can i code a reg exp pattern in php, that replaces each character
    in the "real text" by an other piece of code, but doesnot change any
    of the syntax statements?

    anybodey got an idea?
    help needed

    andreas
  • andreas kirschner

    #2
    Re: replace text outside html syntax regexp php

    Hello again,

    I did it with a for-loop:

    function parse2ImageLett ers($content){
    global $imageLetters;
    $contentArray = array();

    $syntax = false;
    $returnContent = "<nobr>";

    for ($i=0; $i<strlen($cont ent); $i++){
    if (substr($conten t, $i, 1) == "<") {
    $syntax = true;
    $returnContent = $returnContent. "</nobr>";
    }
    if ($syntax == true){
    $returnContent = $returnContent. substr($content , $i, 1);
    if (substr($conten t, $i, 1) == ">"){
    $syntax = false;
    $returnContent = $returnContent. "<nobr>";
    continue;
    }
    else {continue;}
    }


    if (array_key_exis ts(substr($cont ent, $i, 1), $imageLetters)) {
    $returnContent = $returnContent. '<img class="letter"
    src="'.$imageLe tters[substr($content , $i, 1)].'">';
    }
    else{
    if (substr($conten t, $i, 1) == " ") {$returnContent =
    $returnContent. "</nobr>&nbsp; <nobr>";}
    else {$returnContent = $returnContent. substr($content , $i, 1);}
    }

    }
    $returnContent = $returnContent. "</nobr>";
    return $returnContent;

    }

    cheers

    Comment

    Working...