Solved

PHP get value at end of string

Posted on 2014-01-21
9
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 110

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

749 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