Jump to content

Remove hyperlinks from string?


$php_mysql$

Recommended Posts

friends this function creates keywords but i am having a issue, if there is a link on my string then the output that this function gives is like:

 

httpwwwsomesitecomdetailsphpid123456

 

now how to make it that if it finds any http://www.somesite.com/details.php?id=123456, www.somesite.com/details.php?id=123456 or somesite.com/details.php?id=123456 than it ignores the Hyperlink and do not show anything on the output. basically ignore any sort of hyperlinks.

 

 

function metakeyword($string){
      $stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');
   
      $string = preg_replace('/\s\s+/i', ' ', $string); // replace whitespace
      $string = trim($string); // trim the string
      $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
      $string = strtolower($string); // make it lowercase
   
      preg_match_all('/\b.*?\b/i', $string, $matchWords);
      $matchWords = $matchWords[0];
      
      foreach ( $matchWords as $key=>$item ) {
          if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
              unset($matchWords[$key]);
          }
      }   
      $wordCountArr = array();
      if ( is_array($matchWords) ) {
          foreach ( $matchWords as $key => $val ) {
              $val = strtolower($val);
              if ( isset($wordCountArr[$val]) ) {
                  $wordCountArr[$val]++;
              } else {
                  $wordCountArr[$val] = 1;
              }
          }
      }
      arsort($wordCountArr);
      $wordCountArr = array_slice($wordCountArr, 0, 15);
      return $wordCountArr;
}

 

Please help me out you genius people

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/263374-remove-hyperlinks-from-string/
Share on other sites

this was my plan A, but I'll take a look at filter_var as plan B

 

<?php
$str = "friends this function creates keywords but i am having a issue, if there is a link on my string then the output that this function gives is like:

httpwwwsomesitecomdetailsphpid123456

now how to make it that if it finds any http://www.somesite.com/details.php?id=123456, www.somesite.com/details.php?id=123456 or somesite.com/details.php?id=123456 than it ignores the Hyperlink and do not show anything on the output. basically ignore any sort of hyperlinks.
";

echo "<p>$str</p>";

$str = preg_replace('/\s\s+/i', ' ', $str); // replace whitespace

$astr = explode(' ', $str);

foreach ($astr as $k => $v) {
    if (strlen($v)<6) continue;        //ignore short  ones
    $res = parse_url($v);
    if (count($res) < 2) continue;
    unset ($astr[$k]);                 // remove if looks like valid url
}
$str = join (" ", $astr);

echo "Results<br />";
echo "<p>$str</p>";
?>

Plan B using

if (filter_var($v, FILTER_VALIDATE_URL))

 

only found

 

http://www.somesite.com/details.php?id=123456

 

My plan A found

 

http://www.somesite.com/details.php?id=123456

www.somesite.com/details.php?id=123456

somesite.com/details.php?id=123456

 

Plan B using

if (filter_var($v, FILTER_VALIDATE_URL))

 

only found

 

http://www.somesite.com/details.php?id=123456

 

My plan A found

 

http://www.somesite.com/details.php?id=123456

www.somesite.com/details.php?id=123456

somesite.com/details.php?id=123456

thanks mate but i am unable to get it to work with my existing function that i posted above?

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.