Forum Moderators: coopster

Message Too Old, No Replies

Removing some text from a string

removing some text from a string

         

starefossen

4:54 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



Hi,

I have this big string stored in a variable but there are some parts I want to remove and they look like this:

rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=15545"

I could done this by str_replace but the problem is that there are many of the and the ac_id is different for everyone. I thought of using a preg_replace but unfortuently I'm not very good at it and it didn't work.

Dos anyone know how I should do this?

-Hans

[edited by: eelixduppy at 5:01 pm (utc) on April 10, 2008]
[edit reason] exemplified [/edit]

eelixduppy

5:38 pm on Apr 10, 2008 (gmt 0)



If you want to remove the string you have given above from a larger string variable, then you could do something like the following:

$finding = 'rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id="';
$pattern = "/".preg_quote($finding)."\d+/i"
$new_str = preg_replace($pattern, '', $string);

starefossen

6:00 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



I get this error Warning: preg_replace() [function.preg-replace]: Unknown modifier '/'

eelixduppy

6:10 pm on Apr 10, 2008 (gmt 0)



Change the following, sorry:

$pattern = "/".preg_quote($finding, '/')."\d+/i"

starefossen

6:38 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



Hmmm.. This is the outoput from $pattern:

/rdf\:about\="http\:\/\/www\.example\.com\/turplanlegger\/activity\.php\?ac_id\="\d+/i

Is it supposed to be like that? With all the \ ?

It didn't remove anything from the $string

eelixduppy

6:42 pm on Apr 10, 2008 (gmt 0)



Yes, because it is escaping the string you are looking for for use in the pattern without messing up the pattern itself. You want to look for that string exactly, so you have to escape all of the special characters that are interpreted as something else in the pattern itself.

>> It didn't remove anything from the $string

It isn't removing anything from $string, it is taking the string from $string, replacing what you want, and storing the new string, with the replacement, in $new_str, so try echoing that out and see what you get. If that doesn't work then there is something wrong with the information you gave me about what you want to remove from the actual string; it may just not make for some reason.

starefossen

7:29 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



The out put from $new_str is identical to the output from $string!

This is the string:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- generator="FeedCreator 1.7.2" -->
<rdf:RDF
xmlns="http://purl.org/rss/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel rdf:about="http://www.example.com/">
<title>DNT turer</title>
<description>Turer fra Den Norske Turistforening</description>
<link>http://www.example.com/rss_activity2.php</link>
<dc:date>2008-04-10T20:57:09+01:00</dc:date>
<items>
<rdf:Seq>
<rdf:li rdf:resource="http://www.example.com/turplanlegger/activity.php?ac_id=15545"/>
<rdf:li rdf:resource="http://www.example.com/turplanlegger/activity.php?ac_id=15547"/>
<rdf:li rdf:resource="http://www.example.com/turplanlegger/activity.php?ac_id=15548"/>
<rdf:li rdf:resource="http://www.example.com/turplanlegger/activity.php?ac_id=16798"/>
<rdf:li rdf:resource="http://www.example.com/turplanlegger/activity.php?ac_id=15551"/>
<rdf:li rdf:resource="http://www.example.com/turplanlegger/activity.php?ac_id=15550"/>
<rdf:li rdf:resource="http://www.example.com/turplanlegger/activity.php?ac_id=15552"/>
<rdf:li rdf:resource="http://www.example.com/turplanlegger/activity.php?ac_id=15553"/>
<rdf:li rdf:resource="http://www.example.com/turplanlegger/activity.php?ac_id=15844"/>
<rdf:li rdf:resource="http://www.example.com/turplanlegger/activity.php?ac_id=15555"/>
</rdf:Seq>
</items>
</channel>
<item rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=15545">
<dc:format>text/html</dc:format>
<dc:date>2008-04-10</dc:date>
<dc:source>http://www.example.com</dc:source>
<dc:creator>DNT</dc:creator>
<title>Kitekurs på Haugastøl</title>
<link>http://www.example.com/turplanlegger/activity.php?ac_id=15545</link>
<description>Hardangervidda, Hardangervidda</description>
</item>
<item rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=15547">
<dc:format>text/html</dc:format>
<dc:date>2008-04-24</dc:date>
<dc:source>http://www.example.com</dc:source>
<dc:creator>DNT</dc:creator>
<title>Skålatårnet</title>
<link>http://www.example.com/turplanlegger/activity.php?ac_id=15547</link>
<description>Vest-Norge</description>
</item>
<item rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=15548">
<dc:format>text/html</dc:format>
<dc:date>2008-04-30</dc:date>
<dc:source>http://www.example.com</dc:source>
<dc:creator>DNT</dc:creator>
<title>Toppturer i Jotunheimen</title>
<link>http://www.example.com/turplanlegger/activity.php?ac_id=15548</link>
<description>Jotunheimen og fjellområdene rundt, Jotunheimen</description>
</item>
<item rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=16798">
<dc:format>text/html</dc:format>
<dc:date>2008-04-30</dc:date>
<dc:source>http://www.example.com</dc:source>
<dc:creator>DNT</dc:creator>
<title>Vilt og vått- surfekurs på Stadt! </title>
<link>http://www.example.com/turplanlegger/activity.php?ac_id=16798</link>
<description>Vest-Norge, Fjordane</description>
</item>
<item rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=15551">
<dc:format>text/html</dc:format>
<dc:date>2008-05-15</dc:date>
<dc:source>http://www.example.com</dc:source>
<dc:creator>DNT</dc:creator>
<title>17. maitog på Hardangerjøkulen!</title>
<link>http://www.example.com/turplanlegger/activity.php?ac_id=15551</link>
<description>Hardangervidda, Hardangervidda</description>
</item>
<item rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=15550">
<dc:format>text/html</dc:format>
<dc:date>2008-05-22</dc:date>
<dc:source>http://www.example.com</dc:source>
<dc:creator>DNT</dc:creator>
<title>Sykkeltur á la ”Tour de France”</title>
<link>http://www.example.com/turplanlegger/activity.php?ac_id=15550</link>
<description>Vest-Norge, Stølsheimen, Bergsdalen og Vossefjellene</description>
</item>
<item rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=15552">
<dc:format>text/html</dc:format>
<dc:date>2008-05-22</dc:date>
<dc:source>http://www.example.com</dc:source>
<dc:creator>DNT</dc:creator>
<title>Fyrtårntur</title>
<link>http://www.example.com/turplanlegger/activity.php?ac_id=15552</link>
<description></description>
</item>
<item rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=15553">
<dc:format>text/html</dc:format>
<dc:date>2008-05-29</dc:date>
<dc:source>http://www.example.com</dc:source>
<dc:creator>DNT</dc:creator>
<title>Vidden by night</title>
<link>http://www.example.com/turplanlegger/activity.php?ac_id=15553</link>
<description>Vest-Norge, Byfjellene i Bergen</description>
</item>
<item rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=15844">
<dc:format>text/html</dc:format>
<dc:date>2008-05-30</dc:date>
<dc:source>http://www.example.com</dc:source>
<dc:creator>DNT</dc:creator>
<title>Klatrehelg i Øygarden</title>
<link>http://www.example.com/turplanlegger/activity.php?ac_id=15844</link>
<description>Vest-Norge</description>
</item>
<item rdf:about="http://www.example.com/turplanlegger/activity.php?ac_id=15555">
<dc:format>text/html</dc:format>
<dc:date>2008-06-05</dc:date>
<dc:source>http://www.example.com</dc:source>
<dc:creator>DNT</dc:creator>
<title>Havkajakkurs</title>
<link>http://www.example.com/turplanlegger/activity.php?ac_id=15555</link>
<description>Vest-Norge</description>
</item>
</rdf:RDF>

?>

Maby it would be easyer to replace the <item rdf:blablabla> with <item>, is that possible?

starefossen

1:09 pm on Apr 18, 2008 (gmt 0)

10+ Year Member



No one?