Solved

PHP get value at end of string

Posted on 2014-01-21
9
406 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 30

Expert Comment

by:Marco Gasi
Comment Utility
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 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 167 total points
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 333 total points
Comment Utility
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
 

Author Comment

by:Victor Kimura
Comment Utility
@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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Victor Kimura
Comment Utility
@Ray,

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

What is that PHP_EOL, btw?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 333 total points
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
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
Comment Utility
@Marco, thank you so much for the answer! Wish I could give you more points. Next time! God bless<><
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
;-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now