0

I have a simple string that should contain a name and a date of birth as below:

$searchParams = '12/12/1978 Bob Smithers';

or alternatively

$searchParams = 'Bob Smithers 12/12/1978';

What I need to do is split the date away from the name if the date exists within the string. I can obtain the date through the code below although this throws a few quirks if ordered as in the first example above.

$splitData=preg_split('/([0-9]{1,2})-([0-9]{1,2})-([0-9]{2,4})$/', $searchParams);

The second example returns an array:

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b>
<i>(size=2)</i>
0 <font color='#888a85'>=&gt;</font> <small>string</small>
<font color='#cc0000'>'bob smithers '</font>
<i>(length=13)</i>
1 <font color='#888a85'>=&gt;</font> <small>string</small>
<font color='#cc0000'>''</font>
<i>(length=0)</i>

What I require is to split the string into two pieces - one containing the date and one containing the name

1
  • preg_split will use the regexp as the delimiter upon which the string will be split - i.e. it will not be returned. Try preg_match_all to get the date, and then remove it from the original string if it is found Commented Dec 20, 2013 at 10:32

5 Answers 5

1

You can use this simple function, that will return dates in array, and rest of the string:

function strip_name_and_date($searchParams) {
    $pattern = '~\d{2}/\d{2}/\d{4}~';
    preg_match_all($pattern, $searchParams, $matches);
    $name = trim(preg_replace($pattern, '', $searchParams));
    return array('name' => $name, 'dates' => $matches[0]);
}

demo

Sign up to request clarification or add additional context in comments.

Comments

1

preg_split allow fourth parameter is flags:

$searchParams = 'Bob Smithers 12-12-1978';

$splitData=preg_split('/\s*([0-9]{1,2}-[0-9]{1,2}-[0-9]{2,4})\s*/', $searchParams, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

var_dump($splitData);

Comments

1

You can use capturing groups to extract the date and then the name followed by it. It is hard to to write a perfect regular expression to account for all possible dates, including leap years. It's better to use PHP's strtotime() to do the validation:

$pieces = array();    
$searchParams = '12/12/1978 Bob Smithers';
if (preg_match('~(\d{2}/\d{2}/\d{4})\s+(.*)~', $searchParams, $matches)) {
    if (strtotime($matches[1])) {
        $pieces = array($matches[1], $matches[2]);
    }
}

print_r($pieces);

Output:

Array
(
    [0] => 12/12/1978
    [1] => Bob Smithers
)

Demo.

Comments

0

A regex free solution [Works for both cases]

<?php
$searchParams = 'Bob Smithers 12/12/1978';
$val=explode(' ',$searchParams);
if(strpos($val[0],'/')!==false)
{
//echo "true";
$date = array_shift($val);
$name= implode(' ',$val);
echo $name.",".$date;
}
else
{
$date = array_pop($val);
$name= implode(' ',$val);
echo $name.",".$date;
}

Comments

0

The easiest solution I can think of:

$str='12/12/1978 Bob Smithers';
$ar=array();
$regex="(?:\d\d?[\/-]){2}\d{2,4}";
preg_match("/".$regex."/",$str,$ar);
array_push($ar,preg_replace("/\s*".$regex."\s*/","",$str));
var_dump($ar);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.