> ## Content Index
> Fetch the complete content index at: https://blog.hinablue.me/llms.txt
> Use this file to discover other available public pages before exploring further.

# [php tech.] 正規表示式筆記
- URL: https://blog.hinablue.me/php-tech-note-regular-expression/
- Published: 2008-07-29T13:02:00.000Z
- Updated: 2026-06-25T10:20:35.000Z
- Author: Hina Chen
- Tags: PHP, Regex, Regular Expression, 正規表示式

利用正規表示式取出標籤的屬性值。

```php
function imageLongDescriptionConvert($m) {
    var_dump($m);
}
 
$content = '<img class="abcde" style="background-color: #fefefe;" alt="abcdefghijklmnop" />';
$__content = preg_replace_callback("~(<img.*alt\\s*=\\s*[\"'])(?<alt>.*)([\"'].*>)~iUs", "imageLongDescriptionConvert", $content);
 
// 顯示結果
array(4) {
  [0]=>
  string(79) "<img class="abcde" style="background-color: #fefefe;" alt="abcdefghijklmnop" />"
  [1]=>
  string(59) "<img class="abcde" style="background-color: #fefefe;" alt=""
  [2]=>
  string(16) "abcdefghijklmnop"
  [3]=>
  string(4) "" />"
}

```