how can i find two consecutive characters without worrying about spaces between them
how can i find two consecutive characters without worrying about spaces between them
i want to find the character 1 and # in a string and then explode. i can use the explode. my problem is i don't know how much space can occur between these two characters.
// This is what is how my string looks / may look like 1 #first (dog) 1 #second (cat) 1 #third (dog) // this is what i want to do $return = str_ireplace('1 #', ',1 #', $string); // this line is the problem because it only searches for 1 # and not 1 # $return = explode(","$return); // this is how i want my output Array ( [0] => 1 #first (dog) [1] => 1 #second (cat) [2] => 1 #third (dog) )
Answer by heybignick for how can i find two consecutive characters without worrying about spaces between them
You could change this line to remove all the spaces:
$return = str_ireplace('1#', ',1 #', str_ireplace(' ','',$string));
Answer by MaxZoom for how can i find two consecutive characters without worrying about spaces between them
You can use preg_split(..) function on your string as below:
$str = "1 #first (dog) 1 #second (cat) 1 #third (dog)"; $regex = "/1\s*#/"; $keywords = preg_split($regex, $str, 0, PREG_SPLIT_NO_EMPTY); print_r($keywords);
EDIT
To capture also the delimiter itself (not sure why) use below :
$regex = "/1\s*#/"; $strFixed = preg_replace($regex, "|\\0", $str); $tokens = array_filter(explode('|', $strFixed)); print_r($tokens);
What the code does:
- Line 1 defines a regex to search for a string that matches
1space(s)#
- Line 2 inserts
|
as a character to separate - Line 3 explodes on the
|
delimiter and filters empty elements - Line 4 prints the array
The output is
Array ( [1] => 1 #first (dog) [2] => 1 #second (cat) [3] => 1 #third (dog) )
Answer by YamiTenshi for how can i find two consecutive characters without worrying about spaces between them
Regular expressions were pretty much made for what you need. You'd simply replace
$return = str_ireplace('1 #', '1 #', $string);
with
$return = preg_replace('/1 +#/', '1 #', $string);
and that'll do what you want it to.
As for a short explanation of the regex:
The /
at the beginning and the end delimit the beginning and end of the regex. The actual regex can be anything in between those slashes. They don't have to be slashes, in fact - any non-whitespace character can be a delimiter, but keep in mind that any occurrence of this character within your regex must be escaped with a backslash. The reason for these delimiters is that the regex can be followed by various modifiers to change how your regex behaves, such as i
for case-insensitive search.
The 1
matches just what it says on the tin - a single numeral one. It matches this anywhere in the string you're searching. The space also matches a literal space character, and the #
matches a literal pound sign.
The +
indicates that the preceding character may not occur just once, but one or more times. So /a+/
will match on a
, or on aa
, or on aaaaaaa
, etc.
So basically this regex matches any string containing a one followed by one or more spaces, followed by a pound sign, and replaces that whole thing with the string '1 #'
.
Regexes are a more complex beast than I can explain here, but basically any time you don't want to match an exact string but a string with parts that may vary between matches, a regex is probably what you need.
You can check http://regexone.com/ for a decent primer on regexes, they're incredibly useful. Want to figure out if something could be a valid postal code? Regex. Want to see if something looks like a phone number? Regex. Want to know if a password contains enough characters, contains at least one character, at least one number, and does not contain the username? Regex. The possibilities are endless.
Answer by Poiz for how can i find two consecutive characters without worrying about spaces between them
UPDATE
You could do that with a combination of array_filter()
, preg_split()
and array_map()
... plus You may as well quick-test it here....
string '1 #first (dog)' (length=46) 1 => string '1 #second (cat)' (length=46) 2 => string '1 #third (dog)' (length=46)
Answer by mike for how can i find two consecutive characters without worrying about spaces between them
How about that one:
$str = '1 #first (dog) 1 #second (cat) 1 #third (dog)'; preg_match_all('/(1[^1]+)/', $yourStr, $matches); $matches = array_map('trim', $matches[0]); print_r($matches);
This regex matches parts that begin with the number 1
then continues to match everything until again number 1
is found.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment