Solved

PHP: Extracting a certain set of characters from a string

Posted on 2013-02-04
4
482 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 50 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:yodercm
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:yodercm
yodercm earned 50 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 109

Accepted Solution

by:
Ray Paseur earned 400 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Find out what you should include to make the best professional email signature for your organization.
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

774 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