-1

I'm creating a simple comment system connected by Steam API. Every Steam user connected in my website can automatically post things. But i'm changing some functions to replace things like the URLs.

My question is: When a user post something like,

"Hello I'm nice, have a look at http://www.cute.com"

Automatically replaces the http:// for the link without changing the http:// in the string.

JustCarty
  • 3,839
  • 5
  • 31
  • 51
Mark
  • 9
  • 2
  • 3
    Well, sounds like you are looking for a regular expression based string replacement. There are millions of examples for that only a google search away. – arkascha Mar 26 '17 at 20:17
  • You can use the mysql `replace` in your `select` for that. It will only replace on the return, not the actual data in your DB. `replace(column, 'http//', 'link')`. Assuming `http//` was literal and not the full link.. – chris85 Mar 26 '17 at 20:38
  • 2
    don't forget the `:` it is `http://` and not `http//` – muescha Mar 27 '17 at 07:55

2 Answers2

1

Maybe something like this?

<?php
$str = "helloo im nice, have a look http://www.cute.com";
echo preg_replace("/http:\/\/(.+)\.(.+)\.(.+)/", "<a href='http://$1.$2.$3'>$1.$2.$3</a>", $str);
?>

This will convert any link into an anchor (or an a tag).

Alternative added

Alternatively, it might be a good idea to add support for https as well. In which case the following might be useful.

<?php
$str = "helloo im nice, have a look http://www.cute.com";
echo preg_replace("/http(s?):\/\/(.+)\.(.+)\.(.+)/", "<a href='http$1://$2.$3.$4'>http$1://$2.$3.$4</a>", $str);
?>

This takes advantage of the ? modifier which means "one or more of the preceding character". In this case it is the "s" character since it is "http" and "https" both match.

Explanation

This uses RegEx (or Regular Expressions) to create this.
The first parameter of the preg_replace function takes the RegEx (I like to test mine here: http://regexr.com/).

All RegExs must start and end with a forward slash. The bits inbetween are as follows.

http: is simply selecting a string that starts with "http:"
\/\/ is called "escaping" and that will select two forward slashes. Since forward slashes are special characters used in RegEx (start and end of a statement) they need to be escaped so that PHP doesn't think the RegEx has ended sooner.
(.+) The brackets are also special characters (though not escaped) and they are known as "capture groups". What this is used for is so that I can see what is between the "http://" and the ".com" (or whatever extension is used). The full stop (or period or ".") character selects anything.
\. Further on the escaping. Since full stop is used as a special character, we have to escape this one. What that means so far is that we are selecting "http://" then anything and then stopping at a full stop.
(.+) Last but not least is the final capture group. This, again selects anything from the string so that have our final capture group and RegEx complete.

Modifiers:

  • ? means "one or more of the preceding character". This means that /tests?/ would match test and tests since s is the preceding character and in the first example we have 0 and in the second there is 1
  • + means "one of more of the preceding character". In this case we are saying one of more of anything which means we expect at least one character to be provided.

The second parameter is our replace part.

In short, the $1 and $2 sections are to reference the two brackets from the above RegEx.

Some further reading

The PHP function I used

More information on Regular Expressions

RegEx capture groups

JustCarty
  • 3,839
  • 5
  • 31
  • 51
0
$string = 'helloo im nice, have a look http://www.cute.com';
$string = str_replace('http://', '', $string);
echo $string;
S M Iftakhairul
  • 1,120
  • 2
  • 19
  • 42