Extracting data from a sring.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bourbon
    New Member
    • Jan 2007
    • 2

    #1

    Extracting data from a sring.

    $string = '<object width="425" height="350">';

    How do I search the above string to find out what the value of width is and also the value of height.

    Thanks in advance
  • michaelb
    Recognized Expert Contributor
    • Nov 2006
    • 534

    #2
    One way of extracting substrings is using one of the regex family functions.
    The example below may not be the best implementation, but it should give you an idea...

    Code:
    $string = '<object width="425" height="350">';
    $expr=" width=\"([0-9]+)\" +height=\"([0-9]+)";
    
    # case insensitive match is good when dealing with HTML tags
    if (eregi($expr, $string, $regs)) {
    	 echo 'width=' . $regs[1] . ' height=' . $regs[2];
    } else {
    	 echo "failed to find a match";
    }
    You may want to look at other regex functions (preg_match, etc)
    http://us2.php.net/manual/en/function.preg-match.php

    Comment

    Working...