Help with url BBcode

Hi guys, first time here. Hope get some help.
I have a problem with this url bbcode. The url works but the urlname that shows is often repeats itself. If its more that 30 letters it will repeat the linkname. I can see the letter 30 in the code but can i put 40 there instead? or can you guys see another problem with the code?

//Url BBCode with auto triming long links
$text = preg_replace('#\[url\]([\r\n]*)(http://|ftp://|https://|ftps://)([^\s\'\"]*?)([\r\n]*)\[/url\]#sie', "'<a href=\'\\2\\3\' target=\'_blank\' title=\'\\2\\3\'>'.trimlink('\\2\\3', 40).(strlen('\\2\\3')>30?substr('\\2\\3', strlen('\\2\\3')-10, strlen('\\2\\3')):'').'</a>'", $text);
$text = preg_replace('#\[url\]([\r\n]*)([^\s\'\"]*?)([\r\n]*)\[/url\]#sie', "'<a href=\'http://\\2\' target=\'_blank\' title=\'\\2\'>'.trimlink('\\2', 30).(strlen('\\2')>30?substr('\\2', strlen('\\2')-10, strlen('\\2')):'').'</a>'", $text);
$text = preg_replace('#\[url=([\r\n]*)(http://|ftp://|https://|ftps://)([^\s\'\"]*?)\](.*?)([\r\n]*)\[/url\]#si', '<a href=\'\2\3\' target=\'_blank\' title=\'\2\3\'>\4</a>', $text);
$text = preg_replace('#\[url=([\r\n]*)([^\s\'\"]*?)\](.*?)([\r\n]*)\[/url\]#si', '<a href=\'http://\2\' target=\'_blank\' title=\'\2\'>\3</a>', $text);
?>

You can change the 30 for a 40, but this might not solve your problem.

Can you please share an example of such unwanted repetition including the

  • input bbcode
  • expected output
  • actual (broken) output

Also your code is calling a function “trimlink” which isn’t a built-in core function.

Therefore please share the code of that function as well.

What is the reason for doing 4 replacements after each other?

Hi thank for answer.

I did try replace all the letter 30 to 40 and it looks like it solved the problem.
The link did short trim the url-name when its more than 30 letters, and sometimes it repeated the end name.
This looks so bad in the board but now it looks good when i replaced the 30 to 40 :slightly_smiling_face:.
We see in the future if this solved all the problem.

The repetition is gone because when the length is below the threshold, nothing gets appended from the last ternary operator.

I would assume that the trimlink function somehow does the same already and the concatenation isn’t needed after all.
Otherwise there isn’t any explanation why it got duplicated.

The trouble will come back when you’re exceeding the 40 I guess.

If that happens, feel free to reach out again, but please include the code from that function then.
We would need to know what it does.

Thanks again

My forum is pretty big but the codes is getting old now and problem after problem showing up. This URL bbcode have been this way for years.
It is now days that this url bbcode dont get used so much. But i think that if it gets used by users it should look ok.

I think also it will duplicate itself after 40 letters too. But this feature is not used much anymore so maybe it will do quite well now.

This site you got looks like a real nice place to hang out and get some help when codes is getting old. I put your site into favorites and in the future i will post some more problems i got :slightly_smiling_face:. Thanks again.

Sponsor our Newsletter | Privacy Policy | Terms of Service