Reputation: 1074
I have a string which would look something like this "1-1-2012 Something, Something"
How could i wrap the date section so the result would end up as <span>1-1-2012</span> Something, Something
Thanks
Upvotes: 0
Views: 1114
Reputation: 18859
preg_replace( '~\d{1,2}-\d{1,2}-\d{4}~', '<span>$0</span>', '1-1-2012 Something something' );
The regular expression ~\d{1,2}-\d{1,2}-\d{4}
looks for one or two integers, followed by -
, followed by another one or two integers, followed by a -
, followed by 4 integers, which matches both "1-1-2012" and "05-10-2012". It then replaces the match with <span>$0</span>
, where $0 will be replaced with the date found.
Upvotes: 1
Reputation: 4461
check this:
$str="1-1-2012 Something, Something";
$str=preg_replace('/\d-\d-\d{4}/', '<span>$0</span>', $str);
echo $str;
UPD
I missed that the first part was date :)
This might be used:
$str=preg_replace('/(:?\d{1,2}-){2}\d{4}/', '<span>$0</span>', $str);
Upvotes: -1
Reputation: 95121
You can try
$str = "11-10-2012 Something, Something";
$str = preg_replace("/\d{1,2}-\d{1,2}-\d{4}/", '<span>$0</span>', $str);
var_dump($str);
Output
string '<span>4-10-2012</span> Something, Something' (length=43)
Upvotes: 1
Reputation: 324650
You could use something like this:
list($a,$b) = explode(" ",$input,2);
$out = "<span>".$a."</span> ".$b;
Upvotes: 4