News:

Wondering if this will always be free?  See why free is better.

Main Menu

preg_replace

Started by Cadish, August 28, 2004, 05:14:57 AM

Previous topic - Next topic

Cadish

Hi, I've some string like this:

...
<body>
<p>Some text</p>
...
</body>
...


How can I replace the string into something like this:


<p>Some text</p>
...


So just everything inside the <body></body> - tag?

Thanks

[Unknown]

$string = str_replace('~^.+?<body[^>]+?>~', '', $string);
$string = str_replace('~</body>.+?$~', '', $string);

That should do it, no?  Simplified, of course...

-[Unknown]

Anguz

#2
I would probably do it like this

$str = preg_replace('~^.*<body>(.*)<\/body>.*$~isU', '$1', $str);

Cristián Lávaque http://cristianlavaque.com

Saleh

str_replace() takes regexp !!??

We don't need a reason to help people

[Unknown]

Quote from: NeverMind on August 28, 2004, 08:13:32 AM
str_replace() takes regexp !!??

Oops, meant preg... same difference :P.  Hey, it was 3:45 am :P.

-[Unknown]

Cadish

Ok, thanks a lot guys!!

Cadish

Hi,

I don't understand why this shouldn't work with a page like this:

....
&nbsp;<a href="/cgi-bin/citybild?SID=0642195b829afc479147d46cc4c1eedf098a4&PROVIDER=anwendung&WMO=06421&LANG=nl" target="daten" onClick="DownWindow('/wb/reise.htm')">Leuven</a><br>
....


I need the number after "SID=". How can I get this number easily?

Thanks!
Cadish

[Unknown]

session_id().

-[Unknown]

Cadish

Actually, this is from another website...

Saleh

then $_GET['SID']; might do, no?!

We don't need a reason to help people

Cadish

No, don't think so

I'll explain my problem: I want to fetch a website, but need to have the SID-number (which is on another, in the way I said). So, imho, the only way to do it is with regexp...

The Mole

Quote from: Cadish on August 31, 2004, 04:35:39 PM
No, don't think so

I'll explain my problem: I want to fetch a website, but need to have the SID-number (which is on another, in the way I said). So, imho, the only way to do it is with regexp...

Try this:

$str = preg_replace('~^.*<a href="\/cgi-bin\/citybild\?SID=(.*)&PROVIDER.*$~isU', '$1', $str);

-the mole

Advertisement: