Solved

PHP get value at end of string

Posted on 2014-01-21
9
419 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 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