PHP - Replacing ereg with preg - php

I'm trying to remove some deprecated code from a site.
Can anyone tell me the preg equivalent of
ereg_replace("<b>","<strong>",$content);
Thanks.

There seems to be no need for regular expressions at all.
a simple str_replace would do:
$cleaned = str_replace ('<b>', '<strong>', $unCleaned);
If you need more complicated replacements, for example checking the attributes, you could do:
$cleaned = preg_replace('/<b(\s[^>]*)?>/', '<strong\\1>', $unCleaned);
But this is by no means perfect; something like <div title="foo->bar"></div> would break the regular expression.

A PCRE equivalent to your ERE regular expression would be:
preg_match("/<b>/", "<strong>", $content)
But as Jacco already noted you don’t need a regular expression at all as you want to replace a constant value.

Related

How can preg_replace #<a>#</a> to <a>#</a>?

I have a HTML code like this:
##user_name
How can remove the first # by using preg_match?
I just want the preg_replace code.
match # which came before <a tag. try following regular expression
preg_replace("/#(<a)/", "$1", '##user_name');
preg_match — Perform a regular expression match
preg_replace — Perform a regular expression search and replace
So I guess you really need the second:
preg_replace('/^#/', '', '##user_name');

Regular expression for contents within <td>and</td>

I need to find a regular expression to use for finding the content within and tags for use in PHP. I have tried...
preg_split("<td>([^\"]*)</td>", $table[0]);
But that gives me the PHP error...
Warning: preg_split(): Unknown modifier '(' in C:\xampp\htdocs\.....
Can anyone tell me what I am doing wrong?
Try this:
preg_match("/<td>([^\"]*)<\/td>/", $table[0], $matches);
But, as a general rule, please, do not try to parse HTML with regexes... :-)
Keep in mind that you need to do some extra work to make sure that the * between <td> and </td> in your regular expression doesn't slurp up entire lines of <td>some text</td>. That's because * is pretty greedy.
To toggle off the greediness of *, you can put a ? after it - this tells it just grab up until the first time it reaches whatever is after the *. So, the regular expression you're looking for is something like:
/<td>(.*?)<\/td>/
Remember, since the regular expression starts and ends with a /, you have to be careful about any / that is inside your regular expression - they have to be escaped. Hence, the \/.
From your regular expression, it looks like you're also trying to exclude any " character that might be between a <td> and </td> - is that correct? If that were the case, you would change the regular expression to use the following:
/<td>([^\"]*?)<\/td>/
But, assuming you don't want to exclude the " character in your matches, your PHP code could look like this, using preg_match_all instead of preg_match.
preg_match_all("/<td>(.*?)<\/td>/", $str, $matches);
print_r($matches);
What you're looking for is in $matches[1].
Use preg_match instead of preg_split
preg_match("|<td>([^<]*)</td>|", $table[0], $m);
print_r($m);
First of all you forgot to wrap regex with delimiters. Also you shouldn't specify closing td tag in regex.
Try the following code. Assuming $table[0] contains html between <table>, </table> tags, it allows to extract any content (including html) from cells of table:
$a_result = array_map(
function($v) { return preg_replace('/<\/td\s*>/i', '', $v); },
array_slice(preg_split('/<td[^>]*>/i', $table[0]), 1)
);

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]".
If you mean find a string within another string without using regex, you can use strpos
if (strpos('hello today', 'hello') !== false) {
// string found
}
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.
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
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.
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.

PHP preg_replace/preg_match vs PHP str_replace

Can anyone give me a quick summary of the differences please?
To my mind, are they both doing the same thing?
str_replace replaces a specific occurrence of a string, for instance "foo" will only match and replace that: "foo". preg_replace will do regular expression matching, for instance "/f.{2}/" will match and replace "foo", but also "fey", "fir", "fox", "f12", etc.
[EDIT]
See for yourself:
$string = "foo fighters";
$str_replace = str_replace('foo','bar',$string);
$preg_replace = preg_replace('/f.{2}/','bar',$string);
echo 'str_replace: ' . $str_replace . ', preg_replace: ' . $preg_replace;
The output is:
str_replace: bar fighters, preg_replace: bar barhters
:)
str_replace will just replace a fixed string with another fixed string, and it will be much faster.
The regular expression functions allow you to search for and replace with a non-fixed pattern called a regular expression. There are many "flavors" of regular expression which are mostly similar but have certain details differ; the one we are talking about here is Perl Compatible Regular Expressions (PCRE).
If they look the same to you, then you should use str_replace.
str_replace searches for pure text occurences while preg_replace for patterns.
I have not tested by myself, but probably worth of testing. But according to some sources preg_replace is 2x faster on PHP 7 and above.
See more here: preg_replace vs string_replace.

regex: converting php code from eregi to preg_match

I'm trying to find the proper regular expression to convert eregi($1,$2) to preg_match("/$1/i",$2)
i need to consider if there will be functions with () in it, and they may appear more then once.
can anyone please provide the proper regular expression to do so ?
thanks
You don't want to use a regular expression to parse code.
You want to use a parser.
Are you trying to modify your source code, since eregi is deprecated? This regex will do the trick:
$source= <<<STR
eregi(\$1, \$2);
eregi('hello', 'world');
STR;
$source2= preg_replace("/eregi\(['\"]*([^\'\"),]+)['\"]*,\s*['\"]*([^'\"),]+)['\"]*\)/", 'preg_match("/$1/i", "$2")', $source);
var_dump($source2);

Categories