str_split is not the right function for this. For your first example, you would be better off using explode():
PHP Code:
$split_string = explode($foo1, ':');
Your second example could never work. Have another look at the strpos manual page. The second argument is the string you want to find the position of. It neither knows nor cares if your string happens to be a regex pattern - it will just look for that literal string. When you want to use regex, you need to use a regex-specific function. In this case, you want preg_split:
str_split is not the right function for this. For your first example, you would be better off using explode():
PHP Code:
$split_string = explode($foo1, ':');
Your second example could never work. Have another look at the strpos manual page. The second argument is the string you want to find the position of. It neither knows nor cares if your string happens to be a regex pattern - it will just look for that literal string. When you want to use regex, you need to use a regex-specific function. In this case, you want preg_split:
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
<title>New ATL Music : Rocko Ft. Gucci Mane, Officer Ross & Soulja Boy – Maybe (Remix)</title>
This is a line in an xml file.
I'm trying to split the string right at the "colon" or whatever the very first non alpha, or non numeric character the user enters. Like if they accidentally enter in a comma or semicolon...
This worked for me, but it doesn't look out for user error (any other non alpha or numeric character)..
Well, "<" and ">" are non-alpha, non-numeric characters. Should they also be excluded in the character class? Or will the separator always be surrounded by a space on each side? What if there is a comma or apostrophe in the first part of the text before the separator? Should you be using SimpleXML or DOM to get just the text without the tags first, then parse it? Inquiring minds want to know.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Well, "<" and ">" are non-alpha, non-numeric characters. Should they also be excluded in the character class? Or will the separator always be surrounded by a space on each side? What if there is a comma or apostrophe in the first part of the text before the separator? Should you be using SimpleXML or DOM to get just the text without the tags first, then parse it? Inquiring minds want to know.
I see what you're saying...there are too many possibilities so the user has to follow some instruction.
So what I'm looking for now is as follows:
Split at any one of these characters: "~ (tilde), : (colon) , ; (semicolon) , - (hyphen), | (..not sure what this is so I'll call it an 'or' statement), & (ampersand), ^ (..not sure what this is either)"..
Well, "<" and ">" are non-alpha, non-numeric characters. Should they also be excluded in the character class? Or will the separator always be surrounded by a space on each side? What if there is a comma or apostrophe in the first part of the text before the separator? Should you be using SimpleXML or DOM to get just the text without the tags first, then parse it? Inquiring minds want to know.
im sorry...i'm just trying to keep it rather simple...I'm trying to split at any non-alpha and non-numeric character regardless of spacing...it doesn't matter what the character is...
I see what you're saying...there are too many possibilities so the user has to follow some instruction.
So what I'm looking for now is as follows:
Split at any one of these characters: "~ (tilde), : (colon) , ; (semicolon) , - (hyphen), | (..not sure what this is so I'll call it an 'or' statement), & (ampersand), ^ (..not sure what this is either)"..
- split regardless of any space...
and that's it...
Try:
PHP Code:
'#\s*[~:;|-]\s*#'
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
im sorry...i'm just trying to keep it rather simple...I'm trying to split at any non-alpha and non-numeric character regardless of spacing...it doesn't matter what the character is...
The issue is that spaces are non-alpha and non-numeric characters. If you want to exclude them from the split, you need to include them in the character class:
By the way, | is called a vertical bar, or a pipe from its common use as the pipe command in Unix (similar to how you called it or). ^ is normally called a carot (or circumflex on a letter), and sometimes called hat in Maths.
Great wit and madness are near allied, and fine a line their bounds divide.
By the way, | is called a vertical bar, or a pipe from its common use as the pipe command in Unix (similar to how you called it or). ^ is normally called a carot (or circumflex on a letter), and sometimes called hat in Maths.
<spelling_police> Caret </spelling_police>
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
The issue is that spaces are non-alpha and non-numeric characters. If you want to exclude them from the split, you need to include them in the character class:
Bookmarks