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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts

    Outputting metacharacters from regex array

    Hey guys,

    I'm trying to get this regex match to output an array with all the characters that were successfully matched, but it's excluding the <h1></h1> characters since those are interpreted as HTML. I know htmlentities could be useful, but I'm not sure how I would implemented within this script ->

    PHP Code:
    $regexMatch preg_match_all($expression$string$matches,PREG_OFFSET_CAPTURE);
      
           if(
    $array 0){
             echo 
    "Sucess";
             echo 
    "<pre>";
             
    print_r($matches);
             echo 
    "</pre>";
         }
         else
          echo 
    "No Match"
    $matches represents the array containing the matched items- how do I get it to display meta-characters as well?

    Here are the outputs
    Code:
    Expression = "/Hello/"
    String = "Hello" 
    
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [0] => Hello
                        [1] => 0
                    )
    
            )
    
    )
    but....
    Code:
    $expression = "/\<h1\>hello\<\/h1\>/"
    $string = "<h1>hello</h1>"
    
    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [0] => 
                                Hello]
    
                        [1] => 0
                    )
    
            )
    
    )

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I fail to see this as an actual problem, but if you want to display this in a visible format, you can walk the array against the htmlspecialchars with a custom function:
    PHP Code:
    function htmlspecialcharsWalk(&$item)
    {
        if (
    is_string($item))
        {
            
    $item htmlspecialchars($item);
        }
    }

    $visible $array;
    array_walk_recursive($visible'htmlspecialcharsWalk');
    printf("<pre>%s</pre>"print_r($visibletrue)); 

  • Users who have thanked Fou-Lu for this post:

    Ahlahn (05-30-2011)

  • #3
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    That worked beautifully- thank you so much. I've never heard of the array_walk_recursive method... very cool.


  •  

    Posting Permissions

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