Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts

    regexp strangely not working

    Hi all i have this:

    PHP Code:
    $line '            $lineTest = trim($line);';
    $line preg_replace("#(\s*)(\$(?:@?|\!|_)?[a-z]+(?:[a-z]|[0-9]|_)*)(\s+)#is""\\1<span class=\"variable\">\\2</span>\\3"$line);

    returns
    Array
    (
        [
    0] =>             $lineTest 
        
    [1] =>             
        [
    2] => $lineTest
        
    [3] =>  

    using a regexp tool the output i receive indicates it works.. but it doesn't
    Trying to get variables.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    First of all, I'd recommend looking at token_get_all - it tokenizes php scripts for you, so you won't accidentally match variable names in '' quotes or other invalid places.

    http://uk2.php.net/manual/en/function.token-get-all.php

    As for your example, it's often best to use regex quoted with apostrophes ', not quotes " to save you having to "double-slash".

    Correct: '#(\s*)(\$(?:@?|\!|_)?[a-z]+(?:[a-z]|[0-9]|_)*)(\s+)#is'
    Or: "#(\\s*)(\\$(?:@?|\\!|_)?[a-z]+(?:[a-z]|[0-9]|_)*)(\\s+)#is"

    Also, are your \s necessary? This won't match "Hi ".$name."!", for example.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •