質問編集履歴
6
ユーザー名の定義が曖昧だったため、スクリーンネームに変更。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
$content というWordPressの投稿の中から、Twitterの
|
3
|
+
$content というWordPressの投稿の中から、Twitterのスクリーンネーム(@から始まり、英数字とアンダーバーで1-15文字)を検索し、それを<a href="https://twitter.com/user">@user</a>に置換する。というプログラムを作っています。
|
4
4
|
|
5
5
|
しかし下のように、文字列の中に予めリンクが含まれると二重にリンクが貼られてしまいます。
|
6
6
|
aタグの中の@userには置換を行わず、aタグの外の@userのみを置換したいです。
|
5
$content修正。
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,12 +11,12 @@
|
|
11
11
|
### 該当のソースコード
|
12
12
|
|
13
13
|
```php
|
14
|
-
$content ='こんにちは、<a href="https://twitter.com/
|
14
|
+
$content ='こんにちは、<a href="https://twitter.com/user1">@user1</a>と、@user2さん。';
|
15
15
|
function replace_tw_user_name_to_link( $content ) {
|
16
16
|
$content = preg_replace( '/@([0-9a-z_]{1,15})/i', '<a href="https://twitter.com/' . '$1' . '">' . '@' . '$1' . '</a>', $content );
|
17
17
|
return $content;
|
18
18
|
}
|
19
19
|
|
20
20
|
echo $content;
|
21
|
-
結果:'こんにちは、<a href="https://twitter.com/
|
21
|
+
結果:'こんにちは、<a href="https://twitter.com/user1"><a href="https://twitter.com/user1">@user1</a></a>と、<a href="https://twitter.com/user2">@user2</a>さん。'
|
22
22
|
```
|
4
$content修正。
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,12 +11,12 @@
|
|
11
11
|
### 該当のソースコード
|
12
12
|
|
13
13
|
```php
|
14
|
-
$content ='こんにちは、<a href="https://twitter.com/user">@user</a>@user2さん。';
|
14
|
+
$content ='こんにちは、<a href="https://twitter.com/user">@user</a>と、@user2さん。';
|
15
15
|
function replace_tw_user_name_to_link( $content ) {
|
16
16
|
$content = preg_replace( '/@([0-9a-z_]{1,15})/i', '<a href="https://twitter.com/' . '$1' . '">' . '@' . '$1' . '</a>', $content );
|
17
17
|
return $content;
|
18
18
|
}
|
19
19
|
|
20
20
|
echo $content;
|
21
|
-
結果:'こんにちは、<a href="https://twitter.com/user"><a href="https://twitter.com/user">@user</a></a><a href="https://twitter.com/user2">@user2</a>さん。'
|
21
|
+
結果:'こんにちは、<a href="https://twitter.com/user"><a href="https://twitter.com/user">@user</a></a>と、<a href="https://twitter.com/user2">@user2</a>さん。'
|
22
22
|
```
|
3
$contentを、aタグありユーザー名と、aタグなしユーザー名を含むものに変更。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
$content という
|
3
|
+
$content というWordPressの投稿の中から、Twitterのユーザー名(@から始まり、英数字とアンダーバーで1-15文字)を検索し、それを<a href="https://twitter.com/user">@user</a>に置換する。というプログラムを作っています。
|
4
4
|
|
5
5
|
しかし下のように、文字列の中に予めリンクが含まれると二重にリンクが貼られてしまいます。
|
6
6
|
aタグの中の@userには置換を行わず、aタグの外の@userのみを置換したいです。
|
@@ -11,12 +11,12 @@
|
|
11
11
|
### 該当のソースコード
|
12
12
|
|
13
13
|
```php
|
14
|
-
$content ='こんにちは、<a href="https://twitter.com/user">@user</a>さん';
|
14
|
+
$content ='こんにちは、<a href="https://twitter.com/user">@user</a>@user2さん。';
|
15
15
|
function replace_tw_user_name_to_link( $content ) {
|
16
16
|
$content = preg_replace( '/@([0-9a-z_]{1,15})/i', '<a href="https://twitter.com/' . '$1' . '">' . '@' . '$1' . '</a>', $content );
|
17
17
|
return $content;
|
18
18
|
}
|
19
19
|
|
20
20
|
echo $content;
|
21
|
-
結果:'こんにちは、<a href="https://twitter.com/user"><a href="https://twitter.com/user">@user</a></a>'
|
21
|
+
結果:'こんにちは、<a href="https://twitter.com/user"><a href="https://twitter.com/user">@user</a></a><a href="https://twitter.com/user2">@user2</a>さん。'
|
22
22
|
```
|
2
WordPressタグを追加
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
1
正規表現タグを追加
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|