SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question preg_replace using the matched keywords

    I am attempting to use preg_match to find and replace based on some parts that are found in the string, I am able to do this by sepparating the script into different patterns but I wonder if this can be done with ony one pattern.

    For instance I strings like this

    {container id=1 class=basic}
    {container class=advanced id=5}

    and I should be translating that into
    <div class="basic"><a href="/index.php?id=1"></a></div>
    <div class="advanced"><a href="/index.php?id=5"></a></div>

    At the moment I am able to do them but they always have to respect the order of
    {container class=someclassname id=someid}

    if by any chance the user enters
    {container id=someid class=someclassname}

    Then the script does not work

    I found that enclosing the pattern in parenthesis then I can do something like
    $replcement='<div class="${1}"><a href="/index.php?id=${2}"></a></div>';

    but if my pattern looks something like this
    ({test) (id=\d*|class=\w*) (id=\d*|class=\w*)(})

    How could I know if they first entered the id or the class name
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Just to qualify, are those target strings actually part of longer strings?

    eg:

    $target = "Start of the string {container id=1 class=basic} and then there is this case
    {container class=advanced id=5} there may be other cases besides";

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    I would assume this is a method of templating, so would assume that the replace is to be done over a considerable block of string.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Me too, but I thought I would ask.

  5. #5
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Just to qualify, are those target strings actually part of longer strings?

    eg:

    $target = "Start of the string {container id=1 class=basic} and then there is this case
    {container class=advanced id=5} there may be other cases besides";
    Yres they are definitely part of longer strings
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Is ID always numeric and class always a string?

    Will that always be the case, or may you in the future decide to permit another attribute?

    Thinks: hang on, isn't id=5 illegal in terms of the dom? hmmmm... changed between html 4.01 and html 5 http://mathiasbynens.be/notes/html5-id-class , now I did not know that.

  7. #7
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yes, yes it is invalid to have id=1 I just typed it like that to make things a little bit easier to understand but in fact I have something like this {container 1 basic} where the integer is always a 1-3 digit number and the other part is always a word with characters from a-z
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by tlacaelelrl View Post
    ... but in fact I have something like this {container 1 basic} where the integer is always a 1-3 digit number and the other part is always a word with characters from a-z
    OK, now I am confused, is the target {container 1 basic} or is it {container id=1 class=basic} ?

  9. #9
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    OK, now I am confused, is the target {container 1 basic} or is it {container id=1 class=basic} ?
    It is {container 1 basic}
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I think one solution is to use a callback function to sort out which is an integer and which is a string.

    PHP Code:
    <?php

    // set up some test strings

    $strings[] = "The string is {container 1 basic} but could also contain {container advanced 99} for example";
    $strings[] = "This should fail {container 1111 basic}"// > 999
    $strings[] = "This should fail {11 basic}"// no container word
    $strings[] = "This should fail {container 11 basic ";  // missing end bracket
    $strings[] = "This should fail { container 11 basic} "// space prior to first bracket
    $strings[] = "This should fail container 11 basic}"// missing first bracket
    $strings[] = "Should THIS fail {container 11 22}, or what should it do then?"// a quandry?
    $strings[] = "Should THIS fail {container silly basic}"// ditto

    // create the callback function
    function make_divs($matches)
    {
    if( (int)
    $matches[1] > ){
      
    $id $matches[1];
      
    $class $matches[2];
    }else{
      
    $id $matches[2];
      
    $class $matches[1];
    }

    return 
    "<div class=$class><a href=index.php?id=$id></a></div>";
    }

    // loop through the test cases, view page source to see the results properly

    foreach( $strings as $string){

          echo  
    preg_replace_callback(
                
    "#{container (\d{1,3}|[a-z]*) ([a-z]*|\d{1,3})}#"
                
    "make_divs",
                
    $string) , PHP_EOL;
    }


    ?>
    At its simplest that should work, but you will notice that the LAST TWO example strings possibly should fail, but do not -- as things stand, in order to keep the solution simple to follow.

    The more varied and real worldly your test strings are the more complex the solution will probably end up becoming.

    I leave it there because it is now up to you to post the test cases showing where you expect to pass and where you expect to fail.

    HTH

  11. #11
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thank you for all your help, the setting will be controlled so there is only one person entering this strings and he seems to be doing it fine so far.
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  12. #12
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Also a slight note there; make sure your person doing the entering doesnt try to enter an ID of 0
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •