Jump to content

Recommended Posts

I created something that searches for links within a chat row, and outputs then as a link. The search method isn't the best, but that doesn't matter much. It also shortens the link if it's a certain width, Eg: http://www.domain.com/something/somethingelse.php?q=something might show as http://www.domai ... mething . It works fine, I'm just wondering if it could be written better since it's a bit..sketchy.

 

$split = explode(' ', $row['chat']);
	for($i = 0;$i < count($split);$i++)
	{
		if(substr($split[$i], 0, 4) == 'http')
		{
			$link_name = $split[$i];
			if(strlen($split[$i]) >= 40)
			{
				$link_name{(floor(strlen($split[$i])/2))} = ' ';
				$link_parts = explode(' ', $link_name);
				$link_parts[0] = substr($link_parts[0], 0, 19);
				$link_parts[1] = substr($link_parts[1], 19);
				$link_name = $link_parts[0] . ' ... ' . $link_parts[1];
			}
			$split[$i] = '<a class="link" href="' . $split[$i] . '" target="_blank">' . $link_name . '</a>';
		}
	}

Link to comment
https://forums.phpfreaks.com/topic/160081-can-this-be-optimized/
Share on other sites

I wudda opted for preg_replace or a simpler substr system.

 

I went with a preg_replace system and got this.

<?php
$line="this is a sample website http not working while http://www.google.com shud work.
Now a more complicated example http://www.google.com/search?q=preg_replace";

$pr = preg_replace('@(http://([^\s?/]*)[^\s]*)@i','<a class="link" href="\1" target="blank">\2</a>',$line);
header('Content-type: text/plain');
echo "{$pr}\n";

?>

 

but I belive a substr system wud be a lot faster. But this is simple and works nice

 

output was

this is a sample website http not working while <a class="link" href="http://wwww.google.com">www.google.com</a> shud work.

Now a more complicated example <a class="link" href="http://www.google.com/search?q=preg_replace">www.google.com</a>

 

 

I don't know what you're trying to do with $link_name, but if it's greater than 40 characters and you want to concatenate the first 19 characters, the last 19 characters and 3 dots, that will end up being more than 40 characters. I suggest you reduce it down to like 10 :D

 

<?php
$line="this is a sample website http not working while http://www.google.com should work. Now a more complicated example http://www.google.com/search?q=preg_replace";

$parts = explode(' ', $line);
foreach ($parts as $key => $part) {
     if (strpos($part, 'http') === 0 && strlen($part) > 39) {
          $parts[$key] = '<a class="link" href="' . $part . '" target="_blank">' . substr($part, 0, 10) . '...' . substr($part, -4, 4) . '</a>';
     }
}
echo $implode(' ', $parts);

Wrote up some more code

<?php
header('Content-type: text/plain');

$line="this is a sample website http not working while http://www.google.com shud work.
Now a more complicated example http://www.google.com/search?q=preg_replace";

function CutName($var,$len=40)
{
    return strlen($var)>$len?(substr($var,0,$len-3).'...'):$var;
}

function prlinks($matches)
{
    return '<a class="link" href="'. $matches[1] .'" target="_blank">'. CutName($matches[1]) .'</a>';
}

    $pr = preg_replace_callback('@(http://[^\s]*)@i','prlinks',$line);

echo "Preg Match\n";
echo "{$pr}\n";

?>

 

:) Theres my entry :-)

 

preg match is pretty flexible, it shud be faster or just as fast as a comparable strpos/substr/implode/explode routine... :)

 

The reason I kept it as different functions, not to make it messy, but to add more granularity to the functions...

Having the CutName as its own function, allows u to use it elsewhere besides the links routine.

 

So I dun see where Messy comes from?

 

I used the fulll schema of http:// so it grabs full urls, instead of of someome saying just http

 

how hard is it to add https thats simple for me

$pr = preg_replace_callback('@(https?://[^\s]*)@i','prlinks',$line);

 

Now how hard is it for the strpos routine u have?

 

complex pattern matching is best for preg, strpos/substr will just add a lot more code. learn the basics of regex, it may take some time, but its well worth it....

 

So u should look at the difference between both codes again, to me the preg is a lot cleaner than the strpos method.

 

If you want to be technical, your regex doesn't do much more than my simple strpos(). So really, where's the "complex" part of this? The only use I see of using regex is if you were to check if a string is of URL format. Your regex doesn't handle that case.

 

And you ended up using substr() too.

Here are 5 tests results with this file:

<?php

$str = 'aslkdfjs awe werqwotw witowt kwl tow wietqowingtanfgabga,wk eonw aekds goweg nwgna http://asdfwoiet.com/asdfiwe ahtwew https://123.123.123 wer http://agoiww.net/awerqwT?Asdasfoie/aiowe iuotwkl i oawet awltoagl ksbigwb kia giawgbwiabgkagubaibg kb dkljg s wsiowoeriwor a dm e rt yqert qwe t qw er w r wqer wqe r qw er qwe r wqer';

$start = microtime(true);
function CutName($var,$len=40)
{
    return strlen($var)>$len?(substr($var,0,$len-3).'...'):$var;
}

function prlinks($matches)
{
    return '<a class="link" href="'. $matches[1] .'" target="_blank">'. CutName($matches[1]) .'</a>';
}

$pr = preg_replace_callback('@(http://[^\s]*)@i','prlinks',$line);

$end = microtime(true);
$diff1 = $end - $start;

echo 'Test 1: ' . $diff1 . " seconds.\n";

$start = microtime(true);
$parts = explode(' ', $line);
foreach ($parts as $key => $part) {
if (strpos($part, 'http') === 0 && strlen($part) > 39) {
	$parts[$key] = '<a class="link" href="' . $part . '" target="_blank">' . substr($part, 0, 10) . '...' . substr($part, -4, 4) . '</a>';
}
}
$end = microtime(true);
$diff2 = $end - $start;

echo 'Test 2: ' . $diff2 . " seconds.\n";

if ($diff2 > $diff1) echo "Test1 is faster.\n";
else echo "Test2 is faster.\n";

 

Results:

[bash]$ php test.php
Test 1: 0.00012400000000001 seconds.
Test 2: 3.6999999999954E-05 seconds.
Test2 is faster.
[bash]$ php test.php
Test 1: 0.0001239999999999 seconds.
Test 2: 3.7999999999982E-05 seconds.
Test2 is faster.
[bash]$ php test.php
Test 1: 0.00011700000000003 seconds.
Test 2: 3.6000000000036E-05 seconds.
Test2 is faster.
[bash]$ php test.php
Test 1: 0.000112 seconds.
Test 2: 3.7000000000065E-05 seconds.
Test2 is faster.
[bash]$ php test.php
Test 1: 0.00012299999999998 seconds.
Test 2: 3.6999999999954E-05 seconds.
Test2 is faster.

 

Maybe perhaps the string is too short?

 

I also noticed I used $implode on my previous code. :D Here's the corrected code. Sorry about that.

<?php
$line="this is a sample website http not working while http://www.google.com should work. Now a more complicated example http://www.google.com/search?q=preg_replace";

$parts = explode(' ', $line);
foreach ($parts as $key => $part) {
     if (strpos($part, 'http') === 0 && strlen($part) > 39) {
          $parts[$key] = '<a class="link" href="' . $part . '" target="_blank">' . substr($part, 0, 10) . '...' . substr($part, -4, 4) . '</a>';
     }
}
echo implode(' ', $parts);

 

 

Edit: defining the functions outside of $start = microtime(true); for Test1 didn't improve the time by much. Test2 is still faster. Well those are my test. Someone else is free to post up their results as well. :)

I think something is wrong I get Some completely different results

<?php
header('Content-type: text/plain');

$line="this is a sample website http not working while http://www.google.com shud work.
Now a more complicated example http://www.google.com/search?q=preg_replace
secure connection https://docs.google.com";

function CutName($var,$len=40)
{
    return ;
}

function prlinks($matches)
{
    return '<a class="link" href="'. $matches[1] .'" target="_blank">'. (strlen($matches[1])>20?(substr($matches[1],0,17).'...'):$matches[1]) .'</a>';
}
$loops=500;

$at=0;
for($i=0;$i<$loops;$i++)
{
    $st=microtime(TRUE);
    $pr = preg_replace_callback('@(https?://[^\s]*)@i','prlinks',$line);
    $at+=(microtime(TRUE)-$st);
}
$at/=$loops;
$at=number_format($at,6);

echo "Preg Match (Avg time = {$at} secs)\n";
echo "{$pr}\n";

$at=0;
for($i=0;$i<$loops;$i++)
{
		$st=microtime(TRUE);
	$parts = explode(' ', $line);
	foreach ($parts as $key => $part) {
   		if (strpos($part, 'http') === 0 && strlen($part) > 39) {
      	$parts[$key] = '<a class="link" href="' . $part . '" target="_blank">' . substr($part, 0, 10) . '...' . substr($part, -4, 4) . '</a>';
   		}
	}
	$ss=implode(' ',$parts);
	$at+=(microtime(TRUE)-$st);
}
$at/=$loops;
$at=number_format($at,6);
echo "\nSubstr (Avg time = {$at} secs)\n";
echo "{$ss}\n";
?>

 

Result

Preg Match (Avg time = 0.000285 secs)

this is a sample website http not working while <a class="link" href="http://www.google.com" target="_blank">http://www.google...</a> shud work.

Now a more complicated example <a class="link" href="http://www.google.com/search?q=preg_replace" target="_blank">http://www.google...</a>

secure connection <a class="link" href="https://docs.google.com" target="_blank">https://docs.goog...</a>

 

Substr (Avg time = 0.000592 secs)

this is a sample website http not working while http://www.google.com shud work.

Now a more complicated example <a class="link" href="http://www.google.com/search?q=preg_replace

secure" target="_blank">http://www...cure</a> connection https://docs.google.com

 

 

Looks like something went sour on yer code, so time to go back and debug, and make yer code more complex.

 

 

Just some updated code

<?php
header('Content-type: text/plain');

$line="this is a sample website http not working while http://www.google.com shud work.
Now a more complicated example http://www.google.com/search?q=preg_replace
secure connection https://docs.google.com
http://www.php.net/ on a newline
mailto links mailto:someone@gmail.com
ftp://yoursite.com
telnet:myfunky.bbs";

function CutName($var,$len=40)
{
    return strlen($var)>$len?(substr($var,0,(($len-3)/2)).'...'.substr($var,(($len-3)/2)-$len)):$var;
}

function prlinks($matches)
{
    return '<a class="link" href="'. $matches[1] .'" target="_blank">'. CutName($matches[4]) .'</a>';
}

$pr = preg_replace_callback('@((https?|ftp|mailto|telnet):(//)?([^\s]{4,}))@i','prlinks',$line);

echo "Preg Match\n";
echo "{$pr}\n";

?>

 

Preg Match

this is a sample website http not working while <a class="link" href="http://www.google.com" target="_blank">www.google.com</a> shud work.

Now a more complicated example <a class="link" href="http://www.google.com/search?q=preg_replace" target="_blank">www.google.com/search?q=preg_replace</a>

secure connection <a class="link" href="https://docs.google.com" target="_blank">docs.google.com</a>

<a class="link" href="http://www.php.net/" target="_blank">www.php.net/</a> on a newline

mailto links <a class="link" href="mailto:someone@gmail.com" target="_blank">someone@gmail.com</a>

<a class="link" href="ftp://yoursite.com" target="_blank">yoursite.com</a>

<a class="link" href="telnet:myfunky.bbs" target="_blank">myfunky.bbs</a>

[/code]

 

thats pretty feature rich for making links now :) Enjoy

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.