Regular expression help

Discussion in 'PHP' started by PHP dev, Oct 24, 2008.

  1. #1
    Regular expression to insert a space in front of all '<' characters but not </div>

    I have tried with one like this
    preg_replace("/<[\/div>]/", " <", $txt);
    Code (markup):
    but not working
     
    PHP dev, Oct 24, 2008 IP
  2. Synchronium

    Synchronium Active Member

    Messages:
    463
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Try:

    
    preg_replace("/<[^\/div>]/", " <", $txt);
    
    PHP:
     
    Synchronium, Oct 24, 2008 IP
  3. PHP dev

    PHP dev Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply but still it is not working.

    $txt = "tst<i> THIS IS TEST</div> pgm";
    $txt = preg_replace("/<[^\/div>]/", " <", $txt);
    Code (markup):
    o/p
    tst<i> THIS IS TEST</div> pgm (need space in front of <i> but not in </div>)
     
    PHP dev, Oct 24, 2008 IP
  4. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    [^\/div>]

    would simply not place a space if it's /,d,i,v OR >
    for a single character
     
    Kyosys, Oct 24, 2008 IP
  5. PHP dev

    PHP dev Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    < < < <i> < < < < < < < < < < < < < </div> < < < < is the out put
     
    PHP dev, Oct 24, 2008 IP
  6. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    did you listen to what I just said?
     
    Kyosys, Oct 24, 2008 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    You need to use a Lookahead Assertion.

    <?php
    $str = array();
    $str[] = '<i>';
    $str[] = '</div>';
    $str[] = '</i>';
    
    foreach($str as &$st)
    {
    	$st = preg_replace('#<(?!/div)#', '&nbsp;<', $st);
    	echo htmlentities($st) . '<br/>';
    }
    
    ?>
    Code (markup):
     
    joebert, Oct 24, 2008 IP
  8. red-sky

    red-sky Peon

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This should do the trick:

    preg_replace("\<~(\/div\>)", " <", $txt);
    Code (markup):
    Hope this helps

    EDIT - A lookahead is not required to do this, plus it takes it much longer when performing lookahead and lookbehind Regex searches, the Regex I provided will match EVERY '<' where it is not followed by '/div>' and replace it with ' <'. But are you aware that this will pick up here as well, selected in bold:

    <p>This is a paragraph</p>
     
    red-sky, Oct 24, 2008 IP
  9. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #9
    You sure about that red-sky ?

    Even if I add pattern delimiters to get past the syntax errors, I still can't get what you posted to work.
    I'm not even sure about the logic behind it.
     
    joebert, Oct 24, 2008 IP
  10. red-sky

    red-sky Peon

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Actually you may be right, I only realised that this was in a PHP subforum, the Regex I wrote was for VB.NET, sorry about that :p
     
    red-sky, Oct 24, 2008 IP
  11. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #11
    Ah ok. That makes sense now. :)

    What is that tilde bit, some sort of NOT flag in vb ?
     
    joebert, Oct 24, 2008 IP
  12. PHP dev

    PHP dev Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    it is not possible to place the entire html tags in array and check because we can't predict the number of html elements in a page.
    any other soln?
     
    PHP dev, Oct 26, 2008 IP
  13. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #13
    I have no idea what you mean.
     
    joebert, Oct 27, 2008 IP
  14. red-sky

    red-sky Peon

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I think he believes his tags have to be in an array to be alidated with a regular expression, you can run the Regex through your whole html source code, it doesn't have to be stored in arrays. Actually I have no idea about Regex's in PHP so im gonna stop confusing people :p. But can you confirm if it's the same with PHP jobert?

    Thats exactly what it does, VB.NET has limited Regex functionality, but of what's there is can be very powerful.
     
    red-sky, Oct 27, 2008 IP
  15. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #15
    Maybe the way I presented the example code was confusing with that array & &nbsp; instead of an actual space ?

    Here's an example using the test string in an earlier post.

    $str = "tst<i> THIS IS TEST</div> pgm";
    $str = preg_replace('#<(?!/div)#', ' <', $str);
    Code (markup):
    Now, if you want it not to match any closing tags, for instance match <i>, <b>, but not </i>, </b>, etc, you can still use close to the same code, substituting the DIV for a character class consisting of letters & a finishing angle-bracket.

    $str = "start<div>tst<i> THIS IS TEST</i></div> pgm";
    $str = preg_replace('#<(?!/[a-z]+>)#', ' <', $str);
    Code (markup):
    Now I really do have no idea what to try if that's not it.
     
    joebert, Oct 27, 2008 IP
  16. PHP dev

    PHP dev Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Its working!
    Thank you
     
    PHP dev, Oct 27, 2008 IP