Solved

PHP: Extracting a certain set of characters from a string

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spacing 5 29
Wordpress update causing pages to crash 1 19
PHP loop not working 4 30
Re-position sub-options beneath the TAB 7 41
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

705 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