Reputation: 2105
In PHP is there an equivalent to preg_match
that does not require the use of regex? There is str_replace()
for preg_replace
. Is there something for preg_match
.
*update * I am only looking to replace a known string with another. Using regex just seems like overkill.
I have the string "This is a [test1], and not a [test2]" and I want to match them with "[test1]" and "[test2]".
Upvotes: 1
Views: 1418
Reputation: 11711
Most developers use preg_match because they want to use the matches (the third parameter which will get set by the function).
I can not think of a function that will return or set the same information, as done with matches.
If however, you are using preg_match without regex then you might not care as much about the matches.
If you are using preg_match to see if there is a "match" and just that then I'd suggest using strpos instead, since it is much more efficient at seeing if one string is found in another.
Upvotes: 0
Reputation: 86416
To answer your question there is some function of PHP without regex
Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.
But they can not replace preg_match
completely at all
Upvotes: 2
Reputation: 5927
Since I am not sure what result you are looking for I can't say if this is exactly what you are looking for.
You can use strpos
to see if an occurrence of one string is in another.
Upvotes: 1
Reputation: 48887
If you mean find a string within another string without using regex, you can use strpos
if (strpos('hello today', 'hello') !== false) {
// string found
}
Upvotes: 5
Reputation: 8509
First, str_replace()
is not replacement for preg_replace()
. Function str_replace()
replaces all occurrences of the search string with the replacement string, preg_replace() replaces content selected by regular expressions (that's not same thing).
A lot of things require regex (and that's good) so you can't simply replace it with single PHP function.
Upvotes: 0