January 6, 201115 yr I need to remove all the code after a certain word or phrase shows up in a string, including the string it was found in, possibly within the tag itself or between tags AND it could show up in multiple different tags like <p> <b>... Would preg_replace be best for this? To find it and just replace everything after and including it with empty code "". eg.. Say the word shows up in a <a href..etc tag, or between <p> and </p>, I dont want any of that code or any other code after it to display. I'm terrible at regex so if anyone could help me here or point me in the right direction that would be great.
January 8, 201115 yr Author Ok I've sort of got it working, but only to remove the string the keyword is on: $string = preg_replace("/<(a|div|b|p)[^>]*>(.*)(wordtosearchfor|orthisone)(.*)<\/(a|div|b|p)>/iU", '',$string);
January 9, 201115 yr http://www.google.co.uk/search?q=bbcode+%2B+preg_replace&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a Not taking the micky, being dead serious. Look through those and it'll give you an idea.
January 11, 201115 yr Author Can anyone tell me why on the following code: <div style="background-color: #eee"> <p>Bla bla bla text1 bla bla bla</p> <div style="background-color: #"> <a href="http://www.test.com/test/test.html">Bla bla bla text2 bla bla bla</a> Bla bla bla text3 bla bla bla </div> </div> "#<a.*text2.*/a>#i" Works to remove the link with empty code in preg_replace, but the following both do nothing. "#<div.*text3.*/div>#i" "#<div.*<a.*a>.*text3.*/div>#i" Tryed with differnet modifiers /s and /m. Just can't see what I'm doing wrong. Edited January 11, 201115 yr by Mike101
January 12, 201115 yr Can anyone tell me why on the following code: <div style="background-color: #eee"> <p>Bla bla bla text1 bla bla bla</p> <div style="background-color: #"> <a href="http://www.test.com/test/test.html">Bla bla bla text2 bla bla bla</a> Bla bla bla text3 bla bla bla </div> </div> "#<a.*text2.*/a>#i" Works to remove the link with empty code in preg_replace, but the following both do nothing. "#<div.*text3.*/div>#i" "#<div.*<a.*a>.*text3.*/div>#i" Tryed with differnet modifiers /s and /m. Just can't see what I'm doing wrong. Perhaps you could explain what it is your trying to do, Then we can find a better solution to solve your problem
January 12, 201115 yr Author Hey, sorry your right I should have been more specific. I'm pulling rows from a MySql database using php with a lot of junk at the bottom which I need to remove, trouble is its all formated differently, but they do have things in common: 1. Its html 2. Junk is always at the bottom 3. Words or phrases exist in the part I want to remove that do not exist in the part I want to keep So I thought preg_replace would be the best solution for this but I'm having trouble getting my head around regular expressions (I'm getting there though, I know more that I did last week!), unless someone can think of a better idea?
January 12, 201115 yr Hey, sorry your right I should have been more specific. I'm pulling rows from a MySql database using php with a lot of junk at the bottom which I need to remove, trouble is its all formated differently, but they do have things in common: 1. Its html 2. Junk is always at the bottom 3. Words or phrases exist in the part I want to remove that do not exist in the part I want to keep So I thought preg_replace would be the best solution for this but I'm having trouble getting my head around regular expressions (I'm getting there though, I know more that I did last week!), unless someone can think of a better idea? are u talking like a filter for words u don't wanna use? if so u can do this function word_filter($string){ $words = "/(word1|word2|word3|ect..)/i"; if(preg_match($words,$string)){ return preg_replace($words,"",$string); } } or if its html u wanna block use strip_tags or better yet use both the above method makes a really good badword filter Edited January 12, 201115 yr by webdesigner93
January 12, 201115 yr Author It's things like links and adverts, so I need to remove the whole thing. eg in: <div style="background-color: #eee"> <p>Bla bla bla text1 bla bla bla</p> <div style="background-color: #"> <a href="http://www.test.com/test/test.html">Bla bla bla text2 bla bla bla</a> Bla bla bla text3 bla bla bla </div> </div> I'd need to remove this part: <div style="background-color: #"> <a href="http://www.test.com/test/test.html">Bla bla bla text2 bla bla bla</a> Bla bla bla text3 bla bla bla </div> Simply by using text2 or text3 as an anchor. Actually the data in the $row I'm pulling from mysql would be formated in maybe 50 different ways, BUT I could know beforehand because I have a column numbering where it cam from, so I could make a preg_replace function with if statements, eg if format_id 15 do this and so on.. But I can't even get my head around the example above and why my code wont work. Any other ideas? Or just stick with it?
January 12, 201115 yr It's things like links and adverts, so I need to remove the whole thing. eg in: <div style="background-color: #eee"> <p>Bla bla bla text1 bla bla bla</p> <div style="background-color: #"> <a href="http://www.test.com/test/test.html">Bla bla bla text2 bla bla bla</a> Bla bla bla text3 bla bla bla </div> </div> I'd need to remove this part: <div style="background-color: #"> <a href="http://www.test.com/test/test.html">Bla bla bla text2 bla bla bla</a> Bla bla bla text3 bla bla bla </div> Simply by using text2 or text3 as an anchor. Actually the data in the $row I'm pulling from mysql would be formated in maybe 50 different ways, BUT I could know beforehand because I have a column numbering where it cam from, so I could make a preg_replace function with if statements, eg if format_id 15 do this and so on.. But I can't even get my head around the example above and why my code wont work. Any other ideas? Or just stick with it? Mmmm im kinda confused if u just wanna remove it why is it getting inserted into mysql in the first place?
January 12, 201115 yr Author It's automaticaly grabbed by a parser, but either way I'd still have the same problem, removing the stuff I dont want parsed/removing the stuff I dont want to output.
January 12, 201115 yr It's automaticaly grabbed by a parser, but either way I'd still have the same problem, removing the stuff I dont want parsed/removing the stuff I dont want to output. Ok i do understand its grabbed but who inserts the stuff u dont want into mysql? cause if ur wanting stuff to not be inserted into the database to start with u need to use some filtering like strip_tags(remove all php and html tags) ect.. then things like badwords,spamwords ect.. u can use the filter i showed u earlier its a pretty simple concept which i think ur making it harder then it is, u should not have to block any words,strings, links,html tags whatever from being parsed u should do all that when the data is first inserted. Edited January 12, 201115 yr by webdesigner93
January 12, 201115 yr Author I thought it'd be easier to edit it by leaving all the tags intact (slightly bigger db but I could live with that), but I'll have a look at my parser script to see if it'd be better to edit from the other end before it even gets entered. Thanks for the input.
January 12, 201115 yr I thought it'd be easier to edit it by leaving all the tags intact (slightly bigger db but I could live with that), but I'll have a look at my parser script to see if it'd be better to edit from the other end before it even gets entered. Thanks for the input. Wow if ur letting anything into ur mysql database just so it will be easier to edit u have HUGEEEEE security risk on ur hand including mysql injections ect..
January 12, 201115 yr Author The parser is a seperate app that access to the web to ping defined sources added by myself to get the info for the db, and the site that access the db only has read functions.
January 12, 201115 yr The parser is a seperate app that access to the web to ping defined sources added by myself to get the info for the db, and the site that access the db only has read functions. Does not matter about parsing ur still inserting the data
January 12, 201115 yr Author Well I'm looking into editing the data before it gets entered now anyway, either way something like trim, htmlentities, strip_tags will help there or some str_replace. But they are trusted sources. I'm still figuring it all out and its far from done.
Create an account or sign in to comment