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 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php string wildcard usage for matching ^-$string

    What is the correct syntax for matching the entire line by finding a string at the end of the line in the php str_ireplace function?

    For instance matching the entire line by finding "the big red dog" at the end of the line. I thought it would be "^.*$the big red dog" but that does not work.

    ^M any text.....the big red dog ^M

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Posts
    235
    Thanks
    6
    Thanked 21 Times in 20 Posts
    Quite unsure of what you are trying to retrieve but here is an example str_ireplace();

    This code:
    PHP Code:
    <?php
    $mycolour 
    'black';
    $bodytag str_ireplace("InsertColour"$mycolour"<body text=\"InsertColour\">");
    echo 
    $bodytag;
    ?>
    Will output this HTML:
    Code:
    <body text="black">
    Hope that helps.
    Last edited by udjamaflip; 06-14-2009 at 12:22 PM. Reason: typo
    Andrew Sharman
    Web designer, developer and programmer.

    If you found my post helpful, why not give thanks! :)

  • #3
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Since you want to match lines in a multiline string, str_ireplace is not the right tool.
    PHP Code:
    $string "some stuff about a blue dog\n";
    $string .= "a line with the big red dog\n";
    $string .= "another one, with the big red dog not quite at the end\n";
    $string .= "and here it is agein: the big red dog";

    preg_match_all('/^.*the big red dog$/im'$string$matches);
    print_r($matches); 
    Or, of course, preg_replace, if you really want to replace those lines with something -- but you didn't say so in your first post, so I don't really know.

  • #4
    New Coder
    Join Date
    Jun 2009
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the tips. The str_ireplace(find,replace,string,count) works fine for my application. For instance:
    str_ireplace("$the big red dog","the big blue cloud","lkjlk l aaljh l sakjh ajh kajsakjh the big red dog")

    will replace "the big red dog" with "the big blue cloud" and leave the rest of the string unchanged.

    Great that works fine. Now let's say I want to replace the whole string by just finding "the big red dog" so that I don't have to specify all the text in front of the "the big red dog".

    So in this case the output will replace the entire string "lkjlk l aaljh l sakjh ajh kajsakjh the big red dog" with "the big blue cloud".

  • #5
    Regular Coder
    Join Date
    Mar 2008
    Posts
    235
    Thanks
    6
    Thanked 21 Times in 20 Posts
    Quote Originally Posted by bigpcman View Post
    Thanks for the tips. The str_ireplace(find,replace,string,count) works fine for my application. For instance:
    str_ireplace("$the big red dog","the big blue cloud","lkjlk l aaljh l sakjh ajh kajsakjh the big red dog")

    will replace "the big red dog" with "the big blue cloud" and leave the rest of the string unchanged.

    Great that works fine. Now let's say I want to replace the whole string by just finding "the big red dog" so that I don't have to specify all the text in front of the "the big red dog".

    So in this case the output will replace the entire string "lkjlk l aaljh l sakjh ajh kajsakjh the big red dog" with "the big blue cloud".
    Sounds like you want stristr (remember the i means it is case-insensetive)

    PHP Code:
    $mystring 'lkjlk l aaljh l sakjh ajh kajsakjh the big red dog';
    if (
    stristr($mystring'the big red dog'))
    {
        
    $mystring 'the big blue cloud';
    }
    echo 
    $mystring
    http://us2.php.net/manual/en/function.stristr.php
    Andrew Sharman
    Web designer, developer and programmer.

    If you found my post helpful, why not give thanks! :)

  • #6
    New Coder
    Join Date
    Jun 2009
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by udjamaflip View Post
    Sounds like you want stristr (remember the i means it is case-insensetive)

    PHP Code:
    $mystring 'lkjlk l aaljh l sakjh ajh kajsakjh the big red dog';
    if (
    stristr($mystring'the big red dog'))
    {
        
    $mystring 'the big blue cloud';
    }
    echo 
    $mystring
    Thanks for trying to help out. But the point of my question is learning how to use wildcard characters specifically in the str_ireplace(find,replace,string,count) function. I appreciate there are other ways to manipulate text.

  • #7
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    There is no such thing as a wildcard in str_ireplace, that's why I told you it's not the right tool for the job.

    The line you showed before
    Code:
    str_ireplace("$the big red dog","the big blue cloud","lkjlk l aaljh l sakjh ajh kajsakjh the big red dog")
    is complete bogus. I guess you think "$" is a wildcard, but that's not the case. "$the" is seen as an (undefined) variable and substituted with an empty string, thus "big red dog" (without "the") is replaced with "the big blue cloud". And to make matters worse, the code doesn't care one bit if the search string occurs at the end of the line or somewhere else.

    Please believe me that what you want for this job are regular expressions. Once you are ready, reread my last post; it shows you how to do it.


  •  

    Posting Permissions

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