Solved

PHP: Extracting a certain set of characters from a string

Posted on 2013-02-04
4
506 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
[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
  • 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: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 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 110

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

627 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