Solved

PHP: Extracting a certain set of characters from a string

Posted on 2013-02-04
4
477 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

15 Experts available now in Live!

Get 1:1 Help Now