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

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');

Related

preg_replace: Regular expression not work

<h1>title 1</h1 w:id="0"/><p>content</p><h1>title 2</h1 w:id="1"/>...
I want to replace w:id="0"/ from </h1 w:id:="0 or 1 ect "/>
I use this code:
preg_replace("</h1 (.*?)>",'',$html)
But it doesn't work anymore
try this
preg_replace("/<\/h1 (.*?)>/i",'</h1>',$html);
You are missing the delimiters on your regex.
A regex needs a 'starting mark' and a similar 'finishing mark', so PHP can interpret it's content as the match, with all it's flags.
Without the recognized delimiters, it's impossible to diferenciate between simple text and a regex.
Try this regex:
#</h1 (.*?)>#
Or:
~</h1 (.*?)>~
PHP supports a few more delimiters, like <> and /.
As a side note, I would suggest the following regex:
~</h1( [^>]+)?>~i

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)
);

Regex match if not after word

I have a regex that's matching urls and converting them into html links.
If the url is already part of a link I don't want to to match, for example:
http://stackoverflow.com/questions/ask
Should match, but:
Stackoverflow
Shouldn't match
How can I create a regex to do this?
If your url matching regular expression is $URL then you can use the following pattern
(?<!href[\"'])$URL
In PHP you'd write
preg_match("/(?<!href[\"'])$URL/", $text, $matches);
You can use a negative lookbehind to assert that the url is not preceded by href="
(?<!href=")
(Your url-matching pattern should go immediately after that.)
This link provides information. The accepted solution is like so:
<a\s
(?:(?!href=|target=|>).)*
href="http://
(?:(?!target=|>).)*
By removing the references to "target" this should work for you.
Try this
/(?:(([^">']+|^)https?\:\/\/[^\s]+))/m

str_replace + regular expression

I have:
http://stackoverflow.com/questions/43242&cat=aa&id=342342
http://stackoverflow.com/questions/43242&cat=aa&body=434&id=232
http://stackoverflow.com/questions/43242&cat=aa&call=2323&id=14143434
i would like receive:
this link without parameter id:
http://stackoverflow.com/questions/43242&cat=aa
http://stackoverflow.com/questions/43242&cat=aa&body=434
http://stackoverflow.com/questions/43242&cat=aa&call=2323
how this make with PHP? str_replace + regular expression?
preg_replace('~&id=[0-9]+~', '', $str);
Use the appropriate function for this, not regular expressions since URL's aren't regular.
You should then split the query part, which can be done with regular expressions, but I'd like to split on & and then filter out the ID part.

PHP - Replacing ereg with preg

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.

Categories