Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP: Extracting a certain set of characters from a string

Posted on 2013-02-04
4
Medium Priority
?
525 Views
Last Modified: 2013-02-04
Hello,

I'm trying to extract a certain number regardless of where it is at in a string returned in PHP from a SMTP reply. Some examples are:

sent (250 2.0.0 Ok: queued as EC81CCC9B0)
sent (Error 5.1.1 Hard Bounce: queued as EC81CCC9B0)

etc. The number I need extracted in these two cases are the 2.0.0 and 5.1.1 for example. The number will always be 5 in length, have 2 dots and 3 numeric numbers in it as provided.


Does anyone have any suggestions on how I can extract this easily, regardless of where it is at? Thanks!
0
Comment
Question by:Valleriani
  • 2
4 Comments
 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 200 total points
ID: 38850351
try like this if the format is fixed with space,

<?php

$str1 = "sent (250 2.0.0 Ok: queued as EC81CCC9B0)";

$pieces = explode(" ", $str1);

//print_r ($pieces);

echo $pieces[2];


// ------------------------


$str2 = "sent (Error 5.1.1 Hard Bounce: queued as EC81CCC9B0)";

$pieces1 = explode(" ", $str2);

//print_r ($pieces1);

echo "<br>".$pieces1[2];

//

?>

Open in new window

0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 38850756
Is there any chance that there would be other dots in the string?

If not, then use this:

$string = "sent (250 2.0.0 Ok: queued as EC81CCC9B0)";
$array = explode(".",$string);
$first=substr( $array[0], strlen($array[0])-1, 1);
$second = $array[1];
$third = substr($array[2], 0, 1);
$newstring = $first.".".$second.".".$third;
echo $newstring;
0
 
LVL 27

Assisted Solution

by:Cornelia Yoder
Cornelia Yoder earned 200 total points
ID: 38850766
If there might be other dots in the string, then  you will need to add checks to $first, $second, and $third to ensure they are single digit integers.

http://www.php.net/manual/en/function.explode.php
http://www.php.net/manual/en/function.strlen.php
http://www.php.net/manual/en/function.substr.php

http://php.net/manual/en/language.operators.string.php (concatenate)
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1600 total points
ID: 38850817
Please see: http://www.laprbass.com/RAY_temp_valleriani.php

<?php // RAY_temp_valleriani.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';

// TEST CASES FROM THE POST AT EE
$testdata = array
( 'sent (250 2.0.0 Ok: queued as EC81CCC9B0)'
, 'sent (Error 5.1.1 Hard Bounce: queued as EC81CCC9B0)'
)
;

// A REGULAR EXPRESSION TO EXTRACT THE SUBSTRINGS
$rgx
= '#'            // REGEX DELIMITER
. '\b'           // WORD BOUNDARY
. '('            // START CAPTURE GROUP
. '\d{1}'        // SINGLE DIGIT
. '\.'           // SINGLE DOT
. '\d{1}'        // SINGLE DIGIT
. '\.'           // SINGLE DOT
. '\d{1}'        // SINGLE DIGIT
. ')'            // ENDOF GROUP
. '\b'           // WORD BOUNDARY
. '#'            // REGEX DELIMITER
;

// TEST EACH CASE
foreach ($testdata as $str)
{
    preg_match_all($rgx, $str, $mat);
    var_dump($mat[1][0]);
}

Open in new window

0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

972 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