Brook Julias
Brook Julias

Reputation: 2105

php what is the equivalent of preg_match but does not require regex?

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

Answers (5)

ghbarratt
ghbarratt

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

Shakti Singh
Shakti Singh

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

MitMaro
MitMaro

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

webbiedave
webbiedave

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

Wh1T3h4Ck5
Wh1T3h4Ck5

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

Related Questions