adrianTNT Posted January 8, 2007 Share Posted January 8, 2007 Hello.I have a php 'comments script' that allows users to submit comments on web sites.I want to filter the string that represents the actual comment and remove any existent URLs posted by users.For example replace "www." with "..." or .com .net with "..." something like that.Anyone knows a function, code block, etc for this?Thank you.- Adrian. Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/ Share on other sites More sharing options...
Jessica Posted January 8, 2007 Share Posted January 8, 2007 str_replacealternatively, to remove all html : strip_tags() Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156037 Share on other sites More sharing options...
effigy Posted January 8, 2007 Share Posted January 8, 2007 Do you want to replace parts of the URL, or the entire URL itself? There are many ways to approach this depending on how detailed you want to be. Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156041 Share on other sites More sharing options...
phporcaffeine Posted January 8, 2007 Share Posted January 8, 2007 Depending on your code and how you are evaluating you comments, it may be easier to use preg_replace() which will lead you down the dark 'regex' path ... Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156045 Share on other sites More sharing options...
StrangeWill Posted January 8, 2007 Share Posted January 8, 2007 [quote author=phpORcaffine link=topic=121560.msg500006#msg500006 date=1168289901]Depending on your code and how you are evaluating you comments, it may be easier to use preg_replace() which will lead you down the dark 'regex' path ...[/quote]Dark?Regex is all powerful and knowing! Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156046 Share on other sites More sharing options...
adrianTNT Posted January 8, 2007 Author Share Posted January 8, 2007 I want to replace complete URL if possible but I think is easyer to just replce the "www." and ".com:, ".net"I will have a look at str_replace and preg_replace() and see if I can do anything, if you have other ideas please let me know. Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156067 Share on other sites More sharing options...
adrianTNT Posted January 8, 2007 Author Share Posted January 8, 2007 ok, I used preg_replace and seems to do what I need but I get an error and I dont understand it:[code]<?php$string = 'visit my site http://www.google.com or www.msn.com or at least yahoo.com';//$patterns = array('/http://www./','/https://www./','/http/','/www./','/.com/','/.net/','/.org/','/.biz/','/.info/','/:\/\//');//echo preg_replace($patterns, ' ... ', $string);//?>[/code] It says:[quote][b]Warning: Unknown modifier '/' in /var/www/vhosts/adriantnt.com/httpdocs/temp.php on line 6Warning: Unknown modifier '/' in /var/www/vhosts/adriantnt.com/httpdocs/temp.php on line 6[/b]visit my site ... ... ... google ... or ... msn ... or at least yahoo ... [/quote] Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156107 Share on other sites More sharing options...
effigy Posted January 8, 2007 Share Posted January 8, 2007 See [url=http://www.phpfreaks.com/forums/index.php/topic,120594.msg496080.html#msg496080]this[/url] post.If you're not using patterns, don't use preg. Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156114 Share on other sites More sharing options...
adrianTNT Posted January 8, 2007 Author Share Posted January 8, 2007 [quote author=effigy link=topic=121560.msg500076#msg500076 date=1168294569]See [url=http://www.phpfreaks.com/forums/index.php/topic,120594.msg496080.html#msg496080]this[/url] post.If you're not using patterns, don't use preg.[/quote]Effigy , why not ? It seems to work.I got rigd of that warning above, I think I had some slashes entered in bad locations.Thank you all for your help. I like this forum :) Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156117 Share on other sites More sharing options...
effigy Posted January 8, 2007 Share Posted January 8, 2007 Good programming is not only about "working," but also about using the right tools; optimization. Are you going to rent a moving van to haul a box of books when you have a Pinto sitting in the drive way? They both work (Pinto jokes aside)....I thought php.net used to have a blurb in the preg_replace docs about using str_replace for fixed replaces, but I could be wrong. They do have one about using strpos or strstr instead of preg_match, because it's faster. Perhaps it doesn't matter between preg_replace and str_replace, and in our days of fast machines it may not even be a big deal (depending on the usage), but, hopefully you get my point :)[tt]</ramble>[/tt] Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156138 Share on other sites More sharing options...
adrianTNT Posted January 8, 2007 Author Share Posted January 8, 2007 [quote author=effigy link=topic=121560.msg500100#msg500100 date=1168296369]Good programming is not only about "working," but also about using the right tools; optimization. Are you going to rent a moving van to haul a box of books when you have a Pinto sitting in the drive way? They both work (Pinto jokes aside)....I thought php.net used to have a blurb in the preg_replace docs about using str_replace for fixed replaces, but I could be wrong. They do have one about using strpos or strstr instead of preg_match, because it's faster. Perhaps it doesn't matter between preg_replace and str_replace, and in our days of fast machines it may not even be a big deal (depending on the usage), but, hopefully you get my point :)[tt]</ramble>[/tt][/quote]yes, I think I get your point but I am a PHP biginner and I am not very familiar with most of the functions. Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156144 Share on other sites More sharing options...
effigy Posted January 8, 2007 Share Posted January 8, 2007 Understood. Here's another way to approach it, especially since you're a beginner, and in fact, it's something very important that I glazed over myself. preg_replace and str_replace are not only different tools, but they speak different "languages." Did you know that a period is a metacharacter in regex? It matches any character (except a new line, which is allowed under certain conditions). All of the periods in your patterns are not periods, they are this metacharacter; to get a literal period, you must backslash it: [tt]\.[/tt]You missed this, and so did I the first time around. Who knows the extent of knowledge someone else might have when working with your code, or if you'll remember this [i]n[/i] weeks/months from now. Quote Link to comment https://forums.phpfreaks.com/topic/33386-how-to-filter-a-string-to-remove-links-inside-user-comments/#findComment-156163 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.