Connecting Tech Pros Worldwide Forums | Help | Site Map

Preg Replace

Bruno Rafael Moreira de Barros
Guest
 
Posts: n/a
#1: Jun 2 '08
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.

Albert Nick
Guest
 
Posts: n/a
#2: Jun 2 '08

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?
Alexey Kulentsov
Guest
 
Posts: n/a
#3: Jun 2 '08

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);
Tim Roberts
Guest
 
Posts: n/a
#4: Jun 2 '08

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.
Alexey Kulentsov
Guest
 
Posts: n/a
#5: Jun 2 '08

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 &lt;br&gt; tag">
Rik Wasmus
Guest
 
Posts: n/a
#6: Jun 2 '08

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 &lt;br&gt;
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
Alexey Kulentsov
Guest
 
Posts: n/a
#7: Jun 2 '08

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.
John Dunlop
Guest
 
Posts: n/a
#8: Jun 2 '08

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
AnrDaemon
Guest
 
Posts: n/a
#9: Jun 27 '08

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 &lt;br&gt; tag">
According to standard, you do not need to escape ">", only "<".


--
Sincerely Yours, AnrDaemon <anrdaemon@freemail.ru>

Jerry Stuckle
Guest
 
Posts: n/a
#10: Jun 27 '08

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 &lt;br&gt; tag">
>
According to standard, you do not need to escape ">", only "<".
>
>
Wrong (again). That's why there is &gt; Otherwise the <meta tag may
end prematurely.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

AnrDaemon
Guest
 
Posts: n/a
#11: Jun 27 '08

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 &lt;br&gt; tag">
>>
>According to standard, you do not need to escape ">", only "<".
Quote:
Wrong (again). That's why there is &gt; Otherwise the <meta tag may
end prematurely.
Welcome to reality!
http://www.rootdir.org/test.html


--
Sincerely Yours, AnrDaemon <anrdaemon@freemail.ru>

John Dunlop
Guest
 
Posts: n/a
#12: Jun 27 '08

re: Preg Replace


AnrDaemon:
Quote:
Greetings, Alexey Kulentsov.
>
Quote:
Here must be <meta name="description" content="This has a &lt;br&gt; 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
Closed Thread