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.
brunormbarros [ Sa, 12 April 2008 20:36 ] [ ID #1941254 ]

Re: Preg Replace

On 4=D4=C213=C8=D5, =C9=CF=CE=E72=CA=B136=B7=D6, Bruno Rafael Moreira de Bar=
ros
<brunormbar... [at] gmail.com> 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.

What you want to do is just removing HTML tags?
Albert Nick [ So, 13 April 2008 08:59 ] [ ID #1941619 ]

Re: Preg Replace

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);
Alexey Kulentsov [ Mo, 14 April 2008 00:54 ] [ ID #1942169 ]

Re: Preg Replace

Alexey Kulentsov <akul [at] inbox.ru> wrote:

>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 <br> tag">

Whoops...
--
Tim Roberts, timr [at] probo.com
Providenza & Boekelheide, Inc.
Tim Roberts [ Fr, 18 April 2008 05:55 ] [ ID #1945376 ]

Re: Preg Replace

Tim Roberts wrote:
> Alexey Kulentsov <akul [at] inbox.ru> wrote:
>
>> 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 <br> tag">
>
> Whoops...
Incorrect HTML.
Here must be <meta name="description" content="This has a <br> tag">
Alexey Kulentsov [ Fr, 18 April 2008 10:36 ] [ ID #1945384 ]

Re: Preg Replace

On Fri, 18 Apr 2008 10:36:27 +0200, Alexey Kulentsov <akul [at] inbox.ru> wro=
te:

> Tim Roberts wrote:
>> Alexey Kulentsov <akul [at] inbox.ru> wrote:
>>
>>> 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 s=
o.
>>>>
>>>> Thanks in advance.
>>> $without_meta=3Dpreg_replace('/<meta[^>]*>/i','',$with_meta);
>> <html>
>> <head>
>> <head> <meta name=3D"description" content=3D"This has a <br> ta=
g">
>> Whoops...
> Incorrect HTML.
> Here must be <meta name=3D"description" content=3D"This has a <br&g=
t; =

> 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, ve=
ry =

nice package.
-- =

Rik Wasmus
luiheidsgoeroe [ Fr, 18 April 2008 10:45 ] [ ID #1945386 ]

Re: Preg Replace

Rik Wasmus wrote:

> 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.
Alexey Kulentsov [ Sa, 19 April 2008 01:49 ] [ ID #1945884 ]

Re: Preg Replace

Alexey Kulentsov:

> Tim Roberts wrote:
>
> > <meta name="description" content="This has a <br> tag">
>
> Incorrect HTML.

No it isn't.

(Yet *another* reason not to reinvent this particular wheel.)

--
Jock
John Dunlop [ Sa, 19 April 2008 11:59 ] [ ID #1945897 ]
PHP » comp.lang.php » Preg Replace

Vorheriges Thema: variable behaviour
Nächstes Thema: Program to upload e-mails.