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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Any way to shorten this preg_match?

    I currently have a code like this:

    $insert = "how are you today? nice to see you again. here is your login ";
    $wwwcheck = $nameinput;
    if(preg_match("/name1|name2|name3|name4/i", $nameinput)) {
    $tpl = str_replace('name1 ', $insert . '<a href="/folder/name1/">name1</a> ', $tpl);
    $tpl = str_replace('name2 ', $insert . '<a href="/folder/name2/">name2</a> ', $tpl);
    $tpl = str_replace('name3 ', $insert . '<a href="/folder/name3/">name3</a> ', $tpl);
    $tpl = str_replace('name4 ', $insert . '<a href="/folder/name4/">name4</a> ', $tpl);
    }

    which has 60 lines and is only going to keep getting bigger. Is there anyway to shrink the principal down to a single line?

    e.g.

    $insert = "how are you today? nice to see you again. here is your login ";
    $wwwcheck = $nameinput;
    if(preg_match("/name1|name2|name3|name4/i", $nameinput)) {
    $tpl = str_replace('$name ', $insert . '<a href="/folder/$name/">$name</a> ', $tpl);
    }

    Really struggling.

    Thanks
    Mike

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,611
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Not tested but something along the lines of the following should work:

    Code:
    $insert = "how are you today? nice to see you again. here is your login ";
    $wwwcheck = $nameinput;
    $tpl = preg_replace("/(name[1-4])/i", $insert . '<a href="/folder/$1/">$1</a> ',$nameinput);
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    I don't know what the $tpl variable contains but I think that the following code will help you.
    PHP Code:
    if (preg_match('/^name\d+$/i'$nameinput))
            
    $tpl preg_replace('/\b' $nameinput '\b/i'$insert '<a href="/folder/' $nameinput '/">' $nameinput '</a> '$tpl); 

  • #4
    Regular Coder
    Join Date
    Apr 2010
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    $temp_page = preg_replace("/name1|name2|name3|name4/i", '<a href="/folder/$1/">$1</a> ',$temp_page);

    did not work. even:

    $temp_page = preg_replace("/name1|name2|name3|name4/i", $1,$temp_page);

    did not work.

    if (preg_match('/^name\d+$/i', $nameinput))
    $tpl = preg_replace('/\b' . $nameinput . '\b/i', $insert . '<a href="/folder/' . $nameinput . '/">' . $nameinput . '</a> ', $tpl);


    did nothing either.

    Here is a simplified/explanatory version of what I want to do:

    $temp_page = preg_replace("/manchester|leeds|bradford|london/i", '<a href="/folder/$1/">$1</a> ',$temp_page);

    Which results in every instance of "Manchester" or "Leeds" in the $temp_page section becoming:

    <a href="/folder/manchester/">Manchester</a>
    <a href="/folder/leeds/">Leeds</a>
    Last edited by instaunt; 12-06-2012 at 08:27 PM.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,611
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by instaunt View Post
    $temp_page = preg_replace("/name1|name2|name3|name4/i", '<a href="/folder/$1/">$1</a> ',$temp_page);

    did not work. even:

    $temp_page = preg_replace("/name1|name2|name3|name4/i", $1,$temp_page);

    did not work.
    Neither of those worked because you didn't define $1 by wrapping the part of the regular expression you want to substitute inside of () like this:

    preg_replace("/(name1|name2|name3|name4)/i", '<a href="/folder/$1/">$1</a> ',$temp_page)
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Regular Coder
    Join Date
    Apr 2010
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    Neither of those worked because you didn't define $1 by wrapping the part of the regular expression you want to substitute inside of () like this:

    preg_replace("/(name1|name2|name3|name4)/i", '<a href="/folder/$1/">$1</a> ',$temp_page)
    thank you!

    Any way to get the URl $1 to be lowercase? I've tried sticking strtolower in there in various forms, but it just breaks the code. Is the $1 usage causing a problem doing anything else with it?

    Thanks again
    Mike

  • #7
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    You could convert every name to lowercase before the preg_replace. One other option is to use the preg_replace_callback function (check the following code).

    PHP Code:
    // needs php >= 5.3.0
    $tpl preg_replace_callback('/\b(name1|name2|name3|name4)\b/i', function($m) use ($insert) { return $insert '<a href="/folder/' strtolower($m[0]) . '/">' $m[0] . '</a> '; }, $tpl); 

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,246
    Thanks
    12
    Thanked 340 Times in 336 Posts
    btw, str_replace() also accepts arrays as input.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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