Home  >  Article  >  Backend Development  >  PHP regular matching for strings

PHP regular matching for strings

墨辰丷
墨辰丷Original
2018-06-08 10:50:487500browse

This article mainly introduces PHP's regular matching of strings. Interested friends can refer to it. I hope it will be helpful to everyone.

The specific implementation method is as follows:

/**
 * is_external_link 检测字符串是否包含外链
 * @param string $text 文字
 * @param string $host 域名
 * @return boolean    false 有外链 true 无外链
 * /
function all_external_link($text = '', $host = '') {
  if (empty($host)) $host = $_SERVER['HTTP_HOST'];
  $reg = '/http(?:s?):\/\/((?:[A-za-z0-9-]+\.)+[A-za-z]{2,4})/';
  preg_match_all($reg, $text, $data);
  $math = $data[1];
  foreach ($math as $value) {
    if($value != $host) return false;
  }
  return true;
}

Summary: The above is the entire content of this article, I hope it can be helpful to everyone’s study .

Related recommendations:

How PHP implements the integration of DISCUZ users

Summary sharing of PHP processing session functions

PHP four basic sorting algorithms and two search algorithms

The above is the detailed content of PHP regular matching for strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn