开发者

Trim/erase/removal links from string with regex

开发者 https://www.devze.com 2023-04-06 07:38 出处:网络
i have a really simple question, that ... is giving me a hedache ... I just want to trim all links (www.link.com) (http://www.link.com) (http://link.com) ... wathever, from a given string with this r

i have a really simple question, that ... is giving me a hedache ...

I just want to trim all links (www.link.com) (http://www.link.com) (http://link.com) ... wathever, from a given string with this regex:

$regex = "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?";

Just turn them in whitespace.

This is my actual code that wont work ...

<?php

$string = 'ads dsa asd asd as da ds www.something.com adss dsad a sdas d';
$pattern = "(http|ftp|https):开发者_StackOverflow\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;/~\+#]*[\w\-\@?^=%&amp;/~\+#])?";

$replacement = ' nothing ';
echo preg_replace($pattern, $replacement, $string);

?>

it keep saying me that

    ( ! ) Warning: preg_replace() [function.preg-replace]: Unknown modifier ':' in C:\wamp\www\ceva.php on line 9
    Call Stack
    #   Time    Memory  Function    Location
    1   0.0005  363504  {main}( )   ..\ceva.php:0
    2   0.0005  363952  preg_replace ( )    ..\ceva.php:9

Any help would be appreciated :D


Patterns for regex's in php need to be surrounded by a delimiter to show where the pattern starts and stops and the modifiers (case insensitive, multiline...etc) start. Most commonly you will see the forward slash as the delimiter, but most any character will work. An example patter might be: /pattern[a-z]*/i. Notice the first character is a forward slash (/) and the pattern itself ends with the same character (/) and after that only modifiers are expected such as i for case insensitive..etc (see: pattern modifiers). Whatever the first character is, is the delimiter and the pattern part must end with the same character as the ending delimiter. What the error you are seeing means is that your first character, which is an open parenthesis, is the delimiter. After the next open parenthesis, everything after that should be a modifier. The error is saying that there is an invalid modifier after the ending delimiter. To fix this, you should be able to just put a delimiter around the regex and it should work. See Delimeters and for a general reference of php regular expressions.

Edit: I was wondering why you were getting an error regarding the colon as the problem character. Apparently php also supports bracket style delimiters such as { and }. With the open parenthesis being the delimiter, the closing parenthesis is the ending delimiter. The following characters are expected to be delimiters and the pattern throws an error when the first character after the closing delimiter, the colon is reached because it is an invalid modifier. TIL.


Looks like it's unhappy with the colons. Try backslash escaping the ones in your regex (so turn ":" into "\:".


This is very very simple but may be enough, it just assumes there's a whitespace after the link

<?php
    $string = 'here goes 1 http://google.com and 2 http://www.google.com and also 3 www.google.com?q=asd& and thats it';
    $pattern = '/((http|ftp|https):\/\/(www\.)?|www\.)[^\s]*/';
    $return = preg_replace($pattern, '<link>', $string);
    var_dump($return);
?>


You forgot the delimiters around the regex:

instead of :

$pattern = "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;/~\+#]*[\w\-\@?^=%&amp;/~\+#])?";

try:

$pattern = "/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-.,@?^=%&amp;\/~\+#]*[\w\-@?^=%&amp;\/~+#])?/";

or

$pattern = "{(ftp|https?)://[-\w]+(\.[-\w]+)+([-\w.,@?^=%&;/~+#]*[-\w@?^=%&;/~+#])?}";
0

精彩评论

暂无评论...
验证码 换一张
取 消