10c08-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 37 行
PHP
37 行
<?php// A function to create warnings on any links:function warn_links($string) { // First, find all the links in the string: preg_match_all('|<a\s+href\s*=\s*([\'"])(.*?)\1>|i', $string, $results, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); // Now, loop over all results, BACKWARDS, to make replacements // Backwards so that our offsets don't start changing on us: foreach (array_reverse($results) as $r) { // [0][1] contains the offset position of the link & [2][0] // contains the URL - So parse the URL to get it's host: $parsed = parse_url($r[2][0]); // Check the host - if it doesn't exist use this server's hostname: $host = isset($parsed['host']) ? $parsed['host'] : $_SERVER['HTTP_HOST']; // Now insert the 'host' warning: $string = substr_replace($string, " [{$host}] ", $r[0][1], 0); } return $string;}// If we had a POST, then output the data for reference:if (count($_POST)) { echo "<p>The data, with link warnings:</p>\n"; echo warn_links($_POST['data']);}?><form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="f1"><p>Enter some data:</p><p><textarea name="data" cols="80" rows="10"><?= @$_POST['data'] ?></textarea></p><p><input type="submit" /></p></form>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?