Solved

PHP get value at end of string

Posted on 2014-01-21
9
416 Views
Last Modified: 2014-01-22
Hi,

I'm just wondering how to get the value after the word "BUILD" in this string:

&TIMESTAMP=2014-01-21T10:23:13Z&CORRELATIONID=d2436eed33962&ACK=Success&VERSION=109&BUILD=8620107

I found this bit of code which works well for finding strings in between words here:
stackoverflow.com/questions/5696412/get-substring-between-two-strings-php

    public function getStringBetween ( $string, $start, $end ) {

        $string = " ".$string;
        $ini = strpos( $string, $start );
        if ($ini == 0) return "";
        $ini += strlen( $start );
        $len = strpos( $string, $end, $ini ) - $ini;
        return substr( $string, $ini, $len );

    }

Open in new window


Handy. But I need to get the value after "BUILD".

Thank you!
0
Comment
Question by:Victor Kimura
  • 4
  • 3
  • 2
9 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39796563
If the string has always the value you want at the end you ca do this:

$str = "&TIMESTAMP=2014-01-21T10:23:13Z&CORRELATIONID=d2436eed33962&ACK=Success&VERSION=109&BUILD=8620107";
$arr = explode("=", $str);
$wanted_value = end($arr);
//or you can use  a single line:
$wanted_value = end(explode("=", $str));

Open in new window

0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 167 total points
ID: 39796581
Oherwise, if BUILD can be in any place within the string you can use a simple regex:

$str = "&TIMESTAMP=2014-01-21T10:23:13Z&CORRELATIONID=d2436eed33962&ACK=Success&VERSION=109&BUILD=8620107";
$regex = "/(?<=BUILD=)\d+/";
$matches = array();
if (preg_match($regex, $str, $matches))
{
	echo "BUILD value is " . $matches[0];
}

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 333 total points
ID: 39796757
Please see http://www.laprbass.com/RAY_temp_vkimura2007.php

I think I would not use a regular expression when PHP already has a built-in function for exactly this sort of thing.

<?php // RAY_temp_vkimura2007.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28343859.html
// REF http://www.php.net/manual/en/function.parse-str.php

// THE STRING
$str = <<<EOD
&TIMESTAMP=2014-01-21T10:23:13Z&CORRELATIONID=d2436eed33962&ACK=Success&VERSION=109&BUILD=8620107
EOD;

echo $str;
echo PHP_EOL;

parse_str($str, $out);
var_dump($out);
echo PHP_EOL;

// SHOW THE DESIRED DATA ELEMENT
echo "THE 'BUILD' IS: " . $out['BUILD'];

Open in new window

0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:Victor Kimura
ID: 39796762
@Marco,

Thanks! that's helpful! =)

I'm wondering what does the greater and equal sign in the regex mean before the BUILD and question mark?
?<=BUILD=
0
 

Author Comment

by:Victor Kimura
ID: 39796796
@Ray,

Oh, I never knew about the parse_str(). Awesome!

What is that PHP_EOL, btw?
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 333 total points
ID: 39796852
PHP _EOL is the context-aware predefined end-of-line character.  Different OS use different line endings like \r and \n and combinations of those things.  PHP is smart enough to give us the right end-of-line for whatever we are running if we use PHP_EOL.  It makes your code more portable.  Another useful thing is DIRECTORY_SEPARATOR which gives you the correct slash for file paths.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39800028
Hi, vkimura. For some strange reason my answer about the real meaning of '?<=BUILD=' has not been posted: I'm sorry.

?<= means: look if for preceeding characters

so the piece of regex means if preceeding characters are BUILD=

Finally, that regex means: get the numbers preceeded by BUILD=

Thanks for points.
Marco
0
 

Author Comment

by:Victor Kimura
ID: 39800372
@Marco, thank you so much for the answer! Wish I could give you more points. Next time! God bless<><
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39800398
;-)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question