こんにちは。
file_get_contentsでファイル取得後
test1.php内のurlの特定部分以降をurlエンコードしたいのですが
どうやったらいいでしょうか。
http://exmple.com/a/b/c/ :以降を取得しurlencodeしたい。
http://exmple.com/a/b/c/ :以降は階層も文字列もランダムです。
test1.php
http://exmple.com/a/b/c/1/ ... http://exmple.com/a/b/c/2/ ... http://exmple.com/d/e/f/3/ ... http://exmple.com/d/e/f/4/ ... http://exmple.com/g/h/i/5/ ... http://exmple.com/g/h/i/6/ ... ...
やってみたこと
test2.php
$html = file_get_contents('test1.php'); $search = array('exmple.com'); $replace = array('EXMPLE.com'); echo str_replace($search,$replace,$subject);
これで全てのexmple.comをEXMPLE.comに置換することまではできたのですが
目的は部分以降のランダムなものに対してだけエンコード置換することなので
まず http://exmple.com/a/b/c/ でマッチさせて
http://exmple.com/a/b/c/ 部分以降をサーチしてurlエンコードできればokです。
やってみたこと その2
test3.php
$html = file_get_contents('test1.php'); $subject = $html; $url = 'http\:\/\/exmple\.com\/a\/b\/c'; $search = array('http\:\/\/exmple\.com\/a\/b\/c'); $replace = array(urlencode($url)); echo str_replace($search,$replace,$subject);
これで
http://exmple.com/a/b/c部分だけ全てurlencodeできました。
でもやりたいことはhttp://exmple.com/a/b/c/ランダム・・・
のランダム・・・部分だけのurlエンコードですが
もしくはhttp://exmple.com/a/b/c/以降ランダム全体でもかまいません。
(もしかするとそのほうが簡単でいいのかもしれません)
http://exmple.com/a/b/c/以降ランダム全体のurlencodeの場合は
$url = 'http://exmple.com/a/b/c/[以降urlが終わるまで]';
という正規表現が欲しいところですが
http://exmple.com/a/b/c/$1としてみても
http://exmple.com/a/b/c/.*/としてみてもできませんでした。
$url = 'http://exmple.com/a/b/c/[以降urlが終わるまで]';の
preg_replace_callbackも一応は試してはみたのですが今の僕の力では
うまく理解できませんでした。
[以降urlが終わるまで]という正規表現はどう書いたらいいのでしょうか?
わかる方、どうか教えてください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/05 21:17
2016/07/15 01:56
2016/07/15 03:01