Announcement

Collapse
No announcement yet.

how to keep regular expressions from affecting html code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • krs-one
    replied
    ahhh, ok

    Ahh, ok, I see now.

    Well, like I said, you could follow this part:

    PHP Code:
    // assume $query contains what they are looking for
    // assume $string is what you are searching through
    if ($query == preg_match("/<.*?>/",$string)) { 
       
    // found a match, don't display it
    } else {
       
    // print the data however you want

    Also, if you put a
    Code:
    <b>
    tag for text inbetween a
    Code:
    <a>
    and
    Code:
    </a>
    , then it will work just fine like so:

    Code:
    <a href="index.html"><b>hello</b> more a href tag stuff</a>
    Hope this helps.

    -Vic

    Leave a comment:


  • Aaron
    replied
    I'll try to explain more clearly.

    Say I have a string containing both html and text. Now say for example that I need to make every letter 'b' in the text portion of that string be bold by replacing the 'b' with '<b>b</b>'. But I need make sure that 'b' is not replaced with '<b>b</b>' when 'b' is located inside of an html tag or between link tags.

    So, if I have:

    $string = "this is an example string with the letter b, the html tag <br>, and a b between html link tags <a href=\"http://test.example.com\">bbbbb</a>";

    I need to convert that string to:

    $string = "this is an example string with the letter <b>b</b>, the html tag <br>, and a <b>b</b> <b>b</b>etween html link tags <a href=\"http://test.example.com\">bbbbb</a>";

    So, I would need to convert any instance of 'b' to '<b>b</b>' that is not between < and > and not between <a ....> and </a>

    Hopefully that makes more sense. Essentially I want to bold a selected substring as long as the substring is not part of the html code.

    Leave a comment:


  • krs-one
    replied
    i would use preg_match()

    Hello.

    Well, there are several ways you can approach this problem.

    The first would be to use preg_match() to match HTML.

    For example, if you wanted to match all HTML tags with attributes, you could do something like this:

    PHP Code:
    if (preg_match("/<*?>/g",$variable) {
       
    // don't print out the html
    } else {
       print 
    "$variable<br>";

    Is that clear enough? I am having trouble understanding your question. Perhaps you could elaborate some more. Thanks.

    Hope this helps.

    -Vic

    Leave a comment:


  • Aaron
    started a topic how to keep regular expressions from affecting html code

    how to keep regular expressions from affecting html code

    hi, I'm having a little trouble figuring out how to basically exempt html code from regular expressions. I have a search function on my site that searches through a mysql table and outputs the results with the search word in bold. The problem is that if the search word is located between link tags for example, the bold tags will break the link. Also, if someone searches for the letter b for instance, when everything is outputted to the browser all < br > tags are broken.

    So, basically, how do you go about using ereg_replace only on text that is not between < and > and that is not between <a href...> and </a>

    thanks in advance.

Related Topics

Collapse

Working...
X