<?php
session_cache_limiter('nocache');
session_start();
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta content="width=device-width initial-scale=1.0 minimum-scale=1.0 maximum-scale=1.0 user-scalable=no" name="viewport">
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css">
<style>
.param {
width: 400px;
}
textarea {
width: 50%;
height: 200px;
}
</style>
<script>
</script>
</head>
<body>
<h1>アンカー取得 正規表現</h1>
<div id="main">
<form method="post">
<p>パターン:<input class="param" type="text" required name="pattern" value="<?= htmlentities($_POST["pattern"]) ?>"></p>
<p>文 字 列:<textarea name="text"><?= $_POST["text"] ?></textarea></p>
<p><input type="submit" name="send" value="送信"></p>
</form>
</div>
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
$real_pattern = html_entity_decode($_POST["pattern"]);
$re = "/{$real_pattern}/sm";
$str = $_POST['text'];
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
foreach( $matches as $i => $list ) {
foreach( $list as $j => $value ) {
$matches[$i][$j] = htmlentities($value);
}
}
print "<pre>";
print_r($matches);
print "</pre>";
}
?>
</body>
</html>