Jump to content

How to filter a string (to remove links inside user comments).


adrianTNT

Recommended Posts

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.
Link to comment
Share on other sites

[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!
Link to comment
Share on other sites

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 6

Warning: 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]
Link to comment
Share on other sites

[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 :)
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.