SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Use preg_match to split resolution variable

    I am still trying to get the hang of my regexs

    Say I have the variable

    $screen_resolution=1024x768;

    How do I use preg_match to give me the width and the height from the variable.

    I reckon it is something like;

    PHP Code:
    $str='1024x768';

    preg_match('^[0-9]+$x'$str$matches);

    echo 
    "$matches"
    thanks

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $String '1024x768';
    $Height $Width 0;
    If(
    Preg_Match('/^(\d+)x(\d+)$/'$String$Matches)){
        
    $Height $Matches[1];
        
    $Width $Matches[2];
    }
    echo 
    'Height: ' $Height '<br />Width: ' $Width
    \d is a digit, it can be 0-9 (so [0-9] is the same as \d). With preg you still need delimiters at the beginning and end. $Matches is an array, the key 0 being the whole match (1024x768), key 1 being the first bracketed section (1024) and key2 being the second bracketed section (768).
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That has to be the fastest reply ever...thanks v much.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    *headshakes*
    PHP Code:
    $str '1024x768';
    list( 
    $width$height ) = preg_split'/[xX]/'$str, -1);
    // or
    $str '1024x768';
    list( 
    $width$height ) = explode'x'$str ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    5 whole minutes!!! I'm getting slow :P

    Though I did have to start up my web server to test the code before I submitted it...



    @Logic:
    $str = 'helloxhello';

    using explode or preg_split doesn't allow for number verification.
    Width: Hello
    Height: Hello.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    ...Though I did have to start up my web server to test the code before I submitted it...
    You should think about getting an editor or IDE that can run a CLI instance of PHP to test run scripts.
    @Logic:
    $str = 'helloxhello';

    using explode or preg_split doesn't allow for number verification.
    Width: Hello
    Height: Hello.
    That is why you run checks on the value.
    Could as easily run those checks like this:
    PHP Code:
    $str '1024x768';
    @list( 
    $width$height ) = preg_split'/[xX]/'$str, -1);

    if ( !isset( 
    $width$height ) )
        
    // Somethig bad!

    settype$width'int');
    settype$height'int' ); 
    Or even:
    PHP Code:
    $arr preg_split'/[xX]/'$str, -1);
    $arr array_padarray_filter$arr'ctype_digit' ), 2); 
    So many ways.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Or even:
    PHP Code:
    sscanf($str'%dx%d'$width$height); 
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

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