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

    ereg_replace question?

    Hi! So I'm trying to use ereg_replace to replace every instance of a string within something else within a larger string... like ya do. Here's where the issue comes in, the string i'm trying to replace with another string has stars (*) in it, and i'm not sure how to do that. For instance:

    $str1 = "test****";
    $str2 = "test1234";
    $conversation = "the man said test**** down by the bay.";

    $output = ereg_replace($str1, $str2, $conversation);

    Apparently, ereg_replace doesn't like finding strings with * in it, because * means something else. How do i circumvent this so i can find all the occurrences of my $str1, above even though it has a * in it?

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    use preg_replace.

    PHP Code:
    $str1 "test****";
    $str1 preg_quote($str1"#");
    $str2 "test1234";
    $conversation "the man said test**** down by the bay.";

    $output preg_replace("#".$str1."#is"$str2$conversation); 
    Last edited by Phil Jackson; 10-22-2009 at 04:12 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!

    What does the preg_quote, and the the # do? and #is? can you explain it a little big please?
    Last edited by icydash; 10-22-2009 at 03:11 PM.

  • #4
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    preg_quote() escapes all regular expression characters in the provided string, and "#".$string1." #is" is just a regular expression you can use other characters like # or @ in place of /
    'i' means that the match is case insensitive and 's' means that the regular expression should be treated as a single line.


  •  

    Posting Permissions

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