Solved

PHP get value at end of string

Posted on 2014-01-21
9
424 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

734 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