sasori Posted August 15, 2013 Share Posted August 15, 2013 I have these three lines of strings ,that appears at the "Front-end" Listed via Carousell - http://carousell.co, Listed via Carousell - http://thecarousell.com Listed via Carousell - http://thecarousell.com/iphone The scenario is like this, I have a list of items, each of them has a description, and those strings appear in the description randomnly based from some criteria. the objective is, to hide these strings, but due to some highligting functionality, like, when someone search the string "Carousell" at the search box, the word "Carousell" appears to be bold , So when I check it in source code through firebug, the strings appears like these Listed via <em>Carousell</em> - http://carousell.co Listed via <em>Carousell</em> - http://thecarousell.com Listed via <em>Carousell</em> - http://thecarousell.com/iphone my first solution was to use str_replace() func, so I ended up like this $strfilth = array( 'Listed via <em>Carousell</em> - http://carousell.co', 'Listed via <em>Carousell</em> - http://thecarousell.com', 'Listed via <em>Carousell</em> - http://thecarousell.com/iphone', '<em>Carousell</em> - http://carousell.co', 'Listed via <em>Carousell</em> -', 'Listed via', '- http://carousell.co', 'Carousell', ); $filteredString = str_replace($strfilth,"",$dirtyString); That code above didn't helped at all So, I thought of using regex for these, but the problem is , how to cater for those two different scenarios ? , can someone help for the reg-ex pattern for those ? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/ Share on other sites More sharing options...
.josh Posted August 15, 2013 Share Posted August 15, 2013 Where are you even getting $dirtyString from? Are you sure it even has your list in it? I ask because you said this is "front end," which is supposed to mean it's happening client-side, like with javascript. If that is the case, then php isn't going to help you here. Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445142 Share on other sites More sharing options...
sasori Posted August 15, 2013 Author Share Posted August 15, 2013 (edited) Where are you even getting $dirtyString from? Are you sure it even has your list in it? I ask because you said this is "front end," which is supposed to mean it's happening client-side, like with javascript. If that is the case, then php isn't going to help you here. I only used $dirtyString variable when I posted the thread, to represent the item string that contains any of those strings that I mentioned that I should hide. yes it's happening in front-end , it will print the string...but the thing is, there is <em> tag when the string Carousell was search via the search box I tried to play with regex $str = "Listed via Carousell - http://carousell.co<br/> Listed via Carousell - http://thecarousell.com<br/> Listed via Carousell - http://thecarousell.com/iphone<br/> Listed via <em>Carousell</em> - http://carousell.co<br/> Listed via <em>Carousell</em> - http://thecarousell.com<br/> Listed via <em>Carousell</em> - http://thecarousell.com/iphone<br/>"; echo $str; echo "<br/><br/>"; $str = preg_replace("/Listed via <.*?>Carousell/","",$str); echo $str; the output went like this Listed via Carousell - http://carousell.co Listed via Carousell - http://thecarousell.com Listed via Carousell - http://thecarousell.com/iphone Listed via Carousell - http://carousell.co Listed via Carousell - http://thecarousell.com Listed via Carousell - http://thecarousell.com/iphone Listed via Carousell - http://carousell.co Listed via Carousell - http://thecarousell.com Listed via Carousell - http://thecarousell.com/iphone - http://carousell.co - http://thecarousell.com - http://thecarousell.com/iphone you see the first 3 didn't get detected at all.. what more if I want to totally hide all those 6 types of strings Edited August 15, 2013 by sasori Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445143 Share on other sites More sharing options...
.josh Posted August 15, 2013 Share Posted August 15, 2013 Okay, if you want to strip everything except the URLs, try this: $str = preg_replace('~^[^-]+-\s*~m','',$str); Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445147 Share on other sites More sharing options...
sasori Posted August 15, 2013 Author Share Posted August 15, 2013 (edited) Okay, if you want to strip everything except the URLs, try this: $str = preg_replace('~^[^-]+-\s*~m','',$str); can you also add the url? ..actually it's not a hyperlink when it gets displayed, it's a plain text in front end Note: that regex you posted will delete all the strings including the other text in the item's description right? ( i just tried it ) ... i only wanted to delete the particular exact strings that I mentioned those 6 types of strings that I mentioned is part of a string , like ., e.g The quick brown fox jumps over the lazy dog near the bank of the river Listed via Carousell - http://carousell.co or The quick brown fox jumps over the lazy dog near the bank of the river Listed via Carousell - http://thecarousell.com I only want a regex to remove these Listed via Carousell - http://carousell.co, Listed via Carousell - http://thecarousell.com Listed via Carousell - http://thecarousell.com/iphone Listed via <em>Carousell</em> - http://carousell.co Listed via <em>Carousell</em> - http://thecarousell.com Listed via <em>Carousell</em> - http://thecarousell.com/iphone because they are part of a text/string Edited August 15, 2013 by sasori Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445148 Share on other sites More sharing options...
.josh Posted August 15, 2013 Share Posted August 15, 2013 umm, not sure what you mean.. do you mean you want to remove the URL too? But then that just leaves an empty string, yes? Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445149 Share on other sites More sharing options...
sasori Posted August 15, 2013 Author Share Posted August 15, 2013 (edited) umm, not sure what you mean.. do you mean you want to remove the URL too? But then that just leaves an empty string, yes? yes and no.. example: the string appears like this The quick brown fox jumps over the lazy dog near the bank of the river Listed via Carousell - http://thecarousell.com I want to remove the Listed via Carousell - http://thecarousell.com of that string,, so that i can display only this The quick brown fox jumps over the lazy dog near the bank of the river but as I mentioned from the start of the thread, there are six types of them that I want to remove Listed via Carousell - http://carousell.co, Listed via Carousell - http://thecarousell.com Listed via Carousell - http://thecarousell.com/iphone Listed via <em>Carousell</em> - http://carousell.co Listed via <em>Carousell</em> - http://thecarousell.com Listed via <em>Carousell</em> - http://thecarousell.com/iphone every item description that I was trying to tell may contain any of those strings above, how to filter those 6 types in a single regex ? here's a screen shot of my problem Edited August 15, 2013 by sasori Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445155 Share on other sites More sharing options...
Solution .josh Posted August 15, 2013 Solution Share Posted August 15, 2013 oh okay, so this stuff is within a larger string. It helps when you mention the full context of the situation in the first place.. $str = preg_replace('~Listed via (<em>)?Carousell(</em>)? - http://(the)?carousell.com?(/iphone)?~i','',$str); Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445158 Share on other sites More sharing options...
.josh Posted August 15, 2013 Share Posted August 15, 2013 ...and it looks like from your screenshot it may or may not have a URL, so try this: $str = preg_replace('~Listed via (<em>)?Carousell(</em>)?(\s*-(\s*http://(the)?carousell.com?(/iphone)?)?)?~i','',$str); Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445160 Share on other sites More sharing options...
sasori Posted August 15, 2013 Author Share Posted August 15, 2013 (edited) oh okay, so this stuff is within a larger string. It helps when you mention the full context of the situation in the first place.. $str = preg_replace('~Listed via (<em>)?Carousell(</em>)? - http://(the)?carousell.com?(/iphone)?~i','',$str); ...and it looks like from your screenshot it may or may not have a URL, so try this: $str = preg_replace('~Listed via (<em>)?Carousell(</em>)?(\s*-(\s*http://(the)?carousell.com?(/iphone)?)?)?~i','',$str); thanks for these , it somehow has helped ease the pain.. now my problem is, some of the item description got wiped out totally Edited August 15, 2013 by sasori Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445164 Share on other sites More sharing options...
.josh Posted August 15, 2013 Share Posted August 15, 2013 There's no way that regex would have completely wiped the description. Are you sure you don't have some old code attempts in place? Are you sure some of those even had a description, other than the carousell text? Can you please post a full dump of what the code looks like from those screenshots, BEFORE the regex? IOW can you post what your $str value actually looks like, BEFORE anything is done to it. Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445167 Share on other sites More sharing options...
sasori Posted August 15, 2013 Author Share Posted August 15, 2013 There's no way that regex would have completely wiped the description. Are you sure you don't have some old code attempts in place? Are you sure some of those even had a description, other than the carousell text? Can you please post a full dump of what the code looks like from those screenshots, BEFORE the regex? IOW can you post what your $str value actually looks like, BEFORE anything is done to it. ok, I guess it has something to do with that solr function thingy or that highlighting function that adds the <em> thing to the keyword which was inputted to the search box , that produces these item search result listing view I cannot post the full dump of the code, I am using a framework, and it will take more post to explain each of things that is being called just to display that item listings view... but anyway, those two regex you've posted were awesome.... I'll mark this thread as solved, at some point .thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/281197-how-to-reg-ex-this-3-different-lines-of-strings/#findComment-1445170 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.