Hi,
I am sure there is a better way to do this, but try this:
PHP Code:
<?php
$text = '720101-11-5903 (A2037135)';
$text=preg_split('/[^a-zA-Z0-9\-]/',$text);
foreach($text as $key => $value) {
if($value == "") {
unset($text[$key]);
}
}
print_r($text);
?>
preg_match will match what you are looking for and put it into an array. Preg_match is a good way to look for a needle in a haystack of code or text. It is handy in the sense that it will not do anything to the string like preg_split or preg_replace or whatever, but the problem is that if you want to match more than the first match in your string/variable then you will need to use preg_match_all, which will store all of your matches into a multidimensional array, which can get a bit more complicated.
In your post you said that you wanted to split the string, so I used preg_split.
If it were me I would not use a regular expression at all and would do something like:
PHP Code:
<?php
$text = '720101-11-5903 (A2037135)';
$text=str_replace(array('(',')'),'',$text);
$text=explode(' ',$text);
print_r($text);
?>
The reason being that str_replace is simpler and requires a lot less processor power. It is more efficient and much more efficient.
Bookmarks