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 9 of 9
  1. #1
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts

    preg_match detection w/ new element

    if I have
    PHP Code:
    $data '[IMG,height=200,width=200]';

    preg_match('/IMG/'); // this will find the IMG in the string, 
    but the question is, how would I detect what height is equal too, even if I put /height/ . ?

    I want to it so I can grab the variables in a string.

    Thanks
    Last edited by sitNsmile; 11-18-2010 at 10:36 AM.

  • #2
    New Coder
    Join Date
    Jul 2008
    Location
    India
    Posts
    39
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Hi,

    why don't you use explode to separate this string by comma.

    your second element in the resulting array will be height=200. You can easily get the value of height from it, using the same logic.

    hope it helps. try out.

  • #3
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by ssonawa View Post
    Hi,

    why don't you use explode to separate this string by comma.

    your second element in the resulting array will be height=200. You can easily get the value of height from it, using the same logic.

    hope it helps. try out.

    Well something like

    PHP Code:

    $data 
    '[IMG,height=200,width=200]';

    explode(',',$data); // will break it down. 
    I wasn't sure if there was some internal php function that could give me the same results more efficiently

  • #4
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    try this
    PHP Code:
    $string '[IMG,height=200,width=200]';
    preg_match('/\[(IMG)\,height\=([0-9]+)\,width\=([0-9]+)\]/'$string$matches);
    print_r($matches
    you must receive next result
    $match[1] = IMG value
    $match[2] = height value
    $match[3] = width value

  • #5
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by poyzn View Post
    try this
    PHP Code:
    $string '[IMG,height=200,width=200]';
    preg_match('/\[(IMG)\,height\=([0-9]+)\,width\=([0-9]+)\]/'$string$matches);
    print_r($matches
    you must receive next result
    $match[1] = IMG value
    $match[2] = height value
    $match[3] = width value
    Thats something I was looking into,
    question about the markings within preg_match or even something you would use in .htaccess, where do you learn those meanings ? like [0-9]..etc

  • #6
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    this is a regular expression. just google it or you can find more info here http://en.wikipedia.org/wiki/Regular_expression

  • #7
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by poyzn View Post
    this is a regular expression. just google it or you can find more info here http://en.wikipedia.org/wiki/Regular_expression
    Okay to continue my quest of this, what would be a good regex for preg_replace after I have my data.

    this needs to just replace the IMG code with $newString,
    making this abstract for me to use this on other elements and no conflicts will cross. Thanks
    PHP Code:

    $string 
    '<table>[IMG,height=200,width=200]</table>';
    preg_match('/\[(IMG)\,height\=([0-9]+)\,width\=([0-9]+)\]/'$string$matches); 
    $newString "<img src='' height=$matches[3] width= $matches[2] />"
    preg_replace("/[IMG([\w]+?)]/",$newString,$string); 
    Last edited by sitNsmile; 11-18-2010 at 11:20 AM.

  • #8
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    I didn.t get what you want to do, but you can try this code
    PHP Code:

    $string 
    '[IMG,height=200,width=200]';
    preg_match('/\[(IMG)\,height\=([0-9]+)\,width\=([0-9]+)\]/'$string$matches);

    $string '<table>[IMG,height=200,width=200]</table>';
    $newString "<img src='' height=$matches[3] width=$matches[2] />";

    echo 
    preg_replace("/\[IMG[^\]]+\]/",$newString,$string); 

  • Users who have thanked poyzn for this post:

    sitNsmile (11-18-2010)

  • #9
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by poyzn View Post
    I didn.t get what you want to do, but you can try this code
    PHP Code:

    $string 
    '[IMG,height=200,width=200]';
    preg_match('/\[(IMG)\,height\=([0-9]+)\,width\=([0-9]+)\]/'$string$matches);

    $string '<table>[IMG,height=200,width=200]</table>';
    $newString "<img src='' height=$matches[3] width=$matches[2] />";

    echo 
    preg_replace("/\[IMG[^\]]+\]/",$newString,$string); 
    That works great. thank you!


  •  

    Posting Permissions

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