SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How do I get the value that it is in front of a word in a string?

    Hello,

    I have a string like this:

    value11
    value2=x2
    value33
    value4=x4&resp1=x y z
    value55

    How do I get the value that it is in front of an element of this string,
    such as value4=?

    Regards,

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If this is literally a string and not a query-string, then use explode.

    PHP Code:
    // explodes a string into an associative array (came from php.net)
    function explode_assoc($glue1$glue2$array)
    {
      
    $array2=explode($glue2$array);
      foreach(
    $array2 as $val)
      {
                
    $pos=strpos($val,$glue1);
                
    $key=substr($val,0,$pos);
                
    $array3[$key] =substr($val,$pos+1,strlen($val));
      }
      return 
    $array3;
    }

    $string "value4=x4&resp1=x y z";
    $array=explode_assoc('=','&',$string);
    print_r($array); 

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If I understand correctly and you want to get 'value4' if you have 'x4', then:
    PHP Code:
    <?php
    function getName($string$value){
        
    $value addslashes($value);
        
    preg_match("/(.*?)={$value}/msi"$string$matches);
        return 
    $matches[1];
    }
    echo 
    getName('value4=x4&resp1=xyz''x4');
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    No, i want to get the value that it is in front of a word in a string.
    So in this case for example, I want to get the value that is in front of
    value4= which would be x4
    and in front of resp1= which would be xyz.

    Regards,

    Quote Originally Posted by arkinstall View Post
    If I understand correctly and you want to get 'value4' if you have 'x4', then:
    PHP Code:
    <?php
    function getName($string$value){
        
    $value addslashes($value);
        
    preg_match("/(.*?)={$value}/msi"$string$matches);
        return 
    $matches[1];
    }
    echo 
    getName('value4=x4&resp1=xyz''x4');
    ?>

    Anoox search engine volunteer

    www.anoox.com

  5. #5
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This does not do the job at all. Because I have multiple words that have = and & in front of them. So again what I want to do is to get the value that
    is in front of a word of a string of words. So in this case for example
    I want to get the value that is in front of value4= which is x4, etc.
    and again there are many other words in this string with = sign in them.

    Regards,

    Quote Originally Posted by simshaun View Post
    If this is literally a string and not a query-string, then use explode.

    PHP Code:
    // explodes a string into an associative array (came from php.net)
    function explode_assoc($glue1$glue2$array)
    {
      
    $array2=explode($glue2$array);
      foreach(
    $array2 as $val)
      {
                
    $pos=strpos($val,$glue1);
                
    $key=substr($val,0,$pos);
                
    $array3[$key] =substr($val,$pos+1,strlen($val));
      }
      return 
    $array3;
    }

    $string "value4=x4&resp1=x y z";
    $array=explode_assoc('=','&',$string);
    print_r($array); 

    Anoox search engine volunteer

    www.anoox.com

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    WorldNews, I think you have mixed up the word 'Front' with 'Back' here.

    value4=x4

    value4 is on the front of =x4, x4 is at the back of value4=.

    So, the code would be:
    PHP Code:
    <?php 
    function getValue($string$name){ 
        
    $name addslashes($name); 
        
    preg_match("/{$name}=([^&]*)/msi"$string$matches); 
        return 
    $matches[1]; 

    echo 
    getValue('value4=x4&resp1=xyz''value4'); 
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    However, there's a build-in method for this so I'd recommend you used that instead of my solution above:
    PHP Code:
    <?php 
    parse_str
    ('value4=x4&resp1=xyz'$values);
    echo 
    $values['value4'];
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, I guess we have different read on Front' with 'Back'

    But in any case the code below does not do this job since what we need
    to happen is to find the value that is in front of a word in a string and
    then return the value that is in front of it.
    So it needs to be something like this:

    $original_string = "value1=x1 value2=x7 item3=x8 value7=h1"

    and say we are looking for value that is in front of "item3=" so then should be
    like this:

    $result1 = function_name('item3=', $original_string);

    which would then result in $result1 = x8

    Regards,


    Quote Originally Posted by arkinstall View Post
    WorldNews, I think you have mixed up the word
    'Front' with 'Back' here.
    value4=x4

    value4 is on the front of =x4, x4 is at the back of value4=.

    So, the code would be:
    PHP Code:
    <?php 
    function getValue($string$name){ 
        
    $name addslashes($name); 
        
    preg_match("/{$name}=([^&]*)/msi"$string$matches); 
        return 
    $matches[1]; 

    echo 
    getValue('value4=x4&resp1=xyz''value4'); 
    ?>

    Anoox search engine volunteer

    www.anoox.com

  9. #9
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Man, look at arkinstalls last post and research. You can do exactly what you need with it.
    PHP Code:

    <?php 
    parse_str
    ('value4=x4&resp1=xyz'$values);
    echo 
    $values['value4'];

    // and if you need a function of it...
    // usage:  getValue('item3', 'value4=x4&resp1=xyz');
    function getValue($key$string){
        
    parse_str($string$values);
        return 
    $values[$key];
    }

    ?>

  10. #10
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    And this does not do the job either, again it needs to be something like:

    $result = parse_str('result=', $res);
    echo '<p>result is = ' . $result . '<p>';

    So again 1st I need to find the occurrence of 'result=' in $res and then
    return the value that is in front, or back as you like , of 'result='.

    Regards,


    Quote Originally Posted by arkinstall View Post
    However, there's a build-in method for this so I'd recommend you used that instead of my solution above:
    PHP Code:
    <?php 
    parse_str
    ('value4=x4&resp1=xyz'$values);
    echo 
    $values['value4'];
    ?>

    Anoox search engine volunteer

    www.anoox.com

  11. #11
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 
    // usage:  getValue('value4', 'value4=x4&resp1=xyz');
    function getValue($key$string){
        
    parse_str($string$values);
        return 
    $values[$key];
    }

    $string 'value4=x4&resp1=xyz';
    $result parse_str('value4'$string);
    echo 
    '<p>result is = ',$result,'<p>';
    ?>

  12. #12
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I tried this again and gain, and it does not do the job

    Again string is like:
    $res is equal to RESULT=0&&RESPMSG=Approved&AUTHCODE=02631C

    So then when I use your code like:

    $result1 = parse_str('RESULT=', $res);
    echo '<p>result is = ',$result1,'<p>';

    I get NO value for $result1.

    Regards,

    Quote Originally Posted by simshaun View Post
    PHP Code:
    <?php 
    // usage:  getValue('value4', 'value4=x4&resp1=xyz');
    function getValue($key$string){
        
    parse_str($string$values);
        return 
    $values[$key];
    }

    $string 'value4=x4&resp1=xyz';
    $result parse_str('value4'$string);
    echo 
    '<p>result is = ',$result,'<p>';
    ?>

    Anoox search engine volunteer

    www.anoox.com

  13. #13
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    How about this then?
    PHP Code:
    <?php 
    function getValue($string$name){ 
        
    $name addslashes($name); 
        
    preg_match("/{$name}([^&]*)/msi"$string$matches); 
        return 
    $matches[1]; 

    echo 
    getValue('value4=x4&resp1=xyz''value4='); 
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  14. #14
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I err'd in my example.

    Try this.

    PHP Code:
    <?php
    // usage:  getValue('value4', 'value4=x4&resp1=xyz');
    function getValue($key$string){
        
    parse_str($string$values);
        return 
    $values[$key];
    }

    $string 'value4=x4&resp1=xyz';
    $result getValue('value4'$string);
    echo 
    '<p>result is = ',$result,'<p>';
    ?>

  15. #15
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ThanX - this works.

    Sheeesh - thanx a million


    Quote Originally Posted by simshaun View Post
    Sorry, I err'd in my example.

    Try this.

    PHP Code:
    <?php
    // usage:  getValue('value4', 'value4=x4&resp1=xyz');
    function getValue($key$string){
        
    parse_str($string$values);
        return 
    $values[$key];
    }

    $string 'value4=x4&resp1=xyz';
    $result getValue('value4'$string);
    echo 
    '<p>result is = ',$result,'<p>';
    ?>

    Anoox search engine volunteer

    www.anoox.com


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
  •