回答編集履歴

5

追記

2016/04/18 10:02

投稿

orange0190
orange0190

スコア1698

test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  foreach ($match as $m){
26
26
 
27
- $the_content= str_replace($m, '<div>'.$m.'</div>', $the_content);
27
+ $the_content= str_replace($m, '<div id="hoge">'.$m.'</div>', $the_content);
28
28
 
29
29
  }
30
30
 

4

修正

2016/04/18 10:02

投稿

orange0190
orange0190

スコア1698

test CHANGED
@@ -7,3 +7,29 @@
7
7
  ```
8
8
 
9
9
  正規表現には正直自信はありませんが、一応[正規表現チェッカー](http://okumocchi.jp/php/re.php)ではマッチしました。
10
+
11
+
12
+
13
+ 修正コード
14
+
15
+ ---
16
+
17
+ 上記コードでは全くダメだったので修正
18
+
19
+ ```PHP
20
+
21
+ preg_match_all('/<a.*target="_blank".*<\/a>/i', $the_content, $matches);
22
+
23
+ foreach ($matches as $match){
24
+
25
+ foreach ($match as $m){
26
+
27
+ $the_content= str_replace($m, '<div>'.$m.'</div>', $the_content);
28
+
29
+ }
30
+
31
+ }
32
+
33
+ ```
34
+
35
+ これでもまだ問題はありますが、一応置換はできるかと思います。

3

修正

2016/04/18 09:53

投稿

orange0190
orange0190

スコア1698

test CHANGED
@@ -6,4 +6,4 @@
6
6
 
7
7
  ```
8
8
 
9
- 正規表現には正直自はありませんが、一応[正規表現チェッカー](http://okumocchi.jp/php/re.php)ではマッチしました。
9
+ 正規表現には正直自はありませんが、一応[正規表現チェッカー](http://okumocchi.jp/php/re.php)ではマッチしました。

2

コードミスを修正

2016/04/18 09:29

投稿

orange0190
orange0190

スコア1698

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ```PHP
4
4
 
5
- $the_content = preg_replace('/^<a[\s\S]*target="_blank"[\s\S]*<\/a>$/', '<div id="hoge">$1</div>', $the_content);
5
+ $the_content = preg_replace('/^<a[\s\S]*target="_blank"[\s\S]*<\/a>$/i', '<div id="hoge">$1</div>', $the_content);
6
6
 
7
7
  ```
8
8
 

1

コードミスを修正

2016/04/18 09:28

投稿

orange0190
orange0190

スコア1698

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ```PHP
4
4
 
5
- $the_content = preg_replace('/^<a[\s\S]*target="_blank"[\s\S]*\/>$/', '<div id="hoge">$1</div>', $the_content);
5
+ $the_content = preg_replace('/^<a[\s\S]*target="_blank"[\s\S]*<\/a>$/', '<div id="hoge">$1</div>', $the_content);
6
6
 
7
7
  ```
8
8