Preg Replace | | |
I have a file with <html><head><meta something...><meta something
else...>. I need to remove that text.
I would use str_ireplace, but the contents of the two metas change
with the file.
I know preg_replace allows you to do that, but how? I have tried to
learn regular expressions, but I am having serious problems doing so.
Thanks in advance. | | | | re: Preg Replace
On 4月13日, 上午2时36分, Bruno Rafael Moreira de Barros
<brunormbar...@gmail.comwrote: Quote:
I have a file with <html><head><meta something...><meta something
else...>. I need to remove that text.
>
I would use str_ireplace, but the contents of the two metas change
with the file.
>
I know preg_replace allows you to do that, but how? I have tried to
learn regular expressions, but I am having serious problems doing so.
>
Thanks in advance.
What you want to do is just removing HTML tags? | | | | re: Preg Replace
Bruno Rafael Moreira de Barros wrote: Quote:
I have a file with <html><head><meta something...><meta something
else...>. I need to remove that text.
>
I would use str_ireplace, but the contents of the two metas change
with the file.
>
I know preg_replace allows you to do that, but how? I have tried to
learn regular expressions, but I am having serious problems doing so.
>
Thanks in advance.
$without_meta=preg_replace('/<meta[^>]*>/i','',$with_meta); | | | | re: Preg Replace
Alexey Kulentsov <akul@inbox.ruwrote: Quote:
>Bruno Rafael Moreira de Barros wrote: Quote:
>I have a file with <html><head><meta something...><meta something
>else...>. I need to remove that text.
>>
>I would use str_ireplace, but the contents of the two metas change
>with the file.
>>
>I know preg_replace allows you to do that, but how? I have tried to
>learn regular expressions, but I am having serious problems doing so.
>>
>Thanks in advance.
>
>$without_meta=preg_replace('/<meta[^>]*>/i','',$with_meta);
<html>
<head>
<head<meta name="description" content="This has a <brtag">
Whoops...
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc. | | | | re: Preg Replace
Tim Roberts wrote: Quote:
Alexey Kulentsov <akul@inbox.ruwrote:
> Quote:
>Bruno Rafael Moreira de Barros wrote: Quote:
>>I have a file with <html><head><meta something...><meta something
>>else...>. I need to remove that text.
>>>
>>I would use str_ireplace, but the contents of the two metas change
>>with the file.
>>>
>>I know preg_replace allows you to do that, but how? I have tried to
>>learn regular expressions, but I am having serious problems doing so.
>>>
>>Thanks in advance.
>$without_meta=preg_replace('/<meta[^>]*>/i','',$with_meta);
>
<html>
<head>
<head<meta name="description" content="This has a <brtag">
>
Whoops...
Incorrect HTML.
Here must be <meta name="description" content="This has a <br> tag"> | | | | re: Preg Replace
On Fri, 18 Apr 2008 10:36:27 +0200, Alexey Kulentsov <akul@inbox.ruwrote: Quote:
Tim Roberts wrote: Quote:
>Alexey Kulentsov <akul@inbox.ruwrote:
>> Quote:
>>Bruno Rafael Moreira de Barros wrote:
>>>I have a file with <html><head><meta something...><meta something
>>>else...>. I need to remove that text.
>>>>
>>>I would use str_ireplace, but the contents of the two metas change
>>>with the file.
>>>>
>>>I know preg_replace allows you to do that, but how? I have tried to
>>>learn regular expressions, but I am having serious problems doing so.
>>>>
>>>Thanks in advance.
>>$without_meta=preg_replace('/<meta[^>]*>/i','',$with_meta);
> <html>
> <head>
> <head<meta name="description" content="This has a <brtag">
> Whoops...
Incorrect HTML.
Here must be <meta name="description" content="This has a <br>
tag">
Indeed, and how many sites have incorrect HTML? A lot more then fully
valid ones... Which is why I'm going te repeat the mantra: "Use a parser
for HTML manipulation or inspectation, not Regex". DOM comes to mind, very
nice package.
--
Rik Wasmus | | | | re: Preg Replace
Rik Wasmus wrote: Quote:
Indeed, and how many sites have incorrect HTML? A lot more then fully
valid ones... Which is why I'm going te repeat the mantra: "Use a parser
for HTML manipulation or inspectation, not Regex". DOM comes to mind,
very nice package.
I think I am in big trouble: sometimes I use Regex-based parser, for
HTML too. :) I know about problem with incorrect attributes and can
write regexp to deal with it. Yes, this is much more easy to load file
into DOM document using loadHTML() then to write complex regexp dealing
with all these incorrect cases. | | | | re: Preg Replace
Alexey Kulentsov: Quote:
Tim Roberts wrote:
> Quote:
<meta name="description" content="This has a <brtag">
>
Incorrect HTML.
No it isn't.
(Yet *another* reason not to reinvent this particular wheel.)
--
Jock | | | | re: Preg Replace
Greetings, Alexey Kulentsov.
In reply to Your message dated Friday, April 18, 2008, 12:36:27, Quote: Quote: Quote:
>>>I have a file with <html><head><meta something...><meta something
>>>else...>. I need to remove that text.
>>>>
>>>I would use str_ireplace, but the contents of the two metas change
>>>with the file.
>>>>
>>>I know preg_replace allows you to do that, but how? I have tried to
>>>learn regular expressions, but I am having serious problems doing so.
>>>>
>>>Thanks in advance.
>>$without_meta=preg_replace('/<meta[^>]*>/i','',$with_meta);
>>
> <html>
> <head>
> <head<meta name="description" content="This has a <brtag">
>>
>Whoops...
Incorrect HTML.
Here must be <meta name="description" content="This has a <br> tag">
According to standard, you do not need to escape ">", only "<".
--
Sincerely Yours, AnrDaemon <anrdaemon@freemail.ru> | | | | re: Preg Replace
AnrDaemon wrote: Quote:
Greetings, Alexey Kulentsov.
In reply to Your message dated Friday, April 18, 2008, 12:36:27,
> Quote: Quote:
>>>>I have a file with <html><head><meta something...><meta something
>>>>else...>. I need to remove that text.
>>>>>
>>>>I would use str_ireplace, but the contents of the two metas change
>>>>with the file.
>>>>>
>>>>I know preg_replace allows you to do that, but how? I have tried to
>>>>learn regular expressions, but I am having serious problems doing so.
>>>>>
>>>>Thanks in advance.
>>>$without_meta=preg_replace('/<meta[^>]*>/i','',$with_meta);
>> <html>
>> <head>
>> <head<meta name="description" content="This has a <brtag">
>>>
>>Whoops...
> Incorrect HTML.
>Here must be <meta name="description" content="This has a <br> tag">
>
According to standard, you do not need to escape ">", only "<".
>
>
Wrong (again). That's why there is > Otherwise the <meta tag may
end prematurely.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. jstucklex@attglobal.net
================== | | | | re: Preg Replace
Greetings, Jerry Stuckle.
In reply to Your message dated Sunday, June 8, 2008, 21:42:36, Quote: Quote: Quote:
>>>>>I have a file with <html><head><meta something...><meta something
>>>>>else...>. I need to remove that text.
>>>>>>
>>>>>I would use str_ireplace, but the contents of the two metas change
>>>>>with the file.
>>>>>>
>>>>>I know preg_replace allows you to do that, but how? I have tried to
>>>>>learn regular expressions, but I am having serious problems doing so.
>>>>>>
>>>>>Thanks in advance.
>>>>$without_meta=preg_replace('/<meta[^>]*>/i','',$with_meta);
>>> <html>
>>> <head>
>>> <head<meta name="description" content="This has a <brtag">
>>>>
>>>Whoops...
>> Incorrect HTML.
>>Here must be <meta name="description" content="This has a <br> tag">
>>
>According to standard, you do not need to escape ">", only "<".
Quote:
Wrong (again). That's why there is > Otherwise the <meta tag may
end prematurely.
Welcome to reality! http://www.rootdir.org/test.html
--
Sincerely Yours, AnrDaemon <anrdaemon@freemail.ru> | | | | re: Preg Replace
AnrDaemon: Quote:
Greetings, Alexey Kulentsov.
> Quote:
Here must be <meta name="description" content="This has a <br> tag">
>
According to standard, you do not need to escape ">", only "<".
I don't know what "standard" you are talking about, but the HTML
specification allows both "<" and ">" in the content attribute.
Content is defined as CDATA, so pretty much anything goes. If you are
talking about the XHTML specification, then yes, "<" needs to be
escaped but ">" doesn't.
--
Jock |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|