Jump to content

help me fix this small part of code


gdfhghjdfghgfhf

Recommended Posts

I use this code to take out all URL in a string.

 

function xcleaner($url) {
  $U = explode(' ',$url);

  $W =array();
  foreach ($U as $k => $u) {
    if (stristr($u,'http') || (count(explode('.',$u)) > 1)) {
      unset($U[$k]);
      return xcleaner( implode(' ',$U));
    }
  }
  return implode(' ',$U);
}

 

The problem is that the last word before the first dot of the string will be removed.... How can i fix that?

 

exemple:

hello my name is bob. i'm so cool

 

will give:

hello my name is i'm so cool
Link to comment
https://forums.phpfreaks.com/topic/177197-help-me-fix-this-small-part-of-code/
Share on other sites

<?php

function xcleaner($url) 
{
$U = explode(' ', $url);

$W =array();
foreach ($U as $k => $u) 
	{
	$W = explode('.', $u);
	if (stristr($u,'http') || (count($W) > 1 && $W[1] != "") || (count($W) > 2))
		{
		unset($U[$k]);
		return implode(' ',$U);
		}
	}
return implode(' ',$U);
}

$Test = 'hello my name is bob. i\'m so cool';
echo xcleaner($Test);

?>

 

When you split "something." by the ., the count will always be two. You'll have something, and then for [1] you'll just have blank. It's just the way it works. I just checked to make sure that [1] actually had something, and then checked if the count was more than 2 (in case you had .., which would mean [1] was blank, but there was still more than 1 count)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.