<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>zaru blog &#187; プログラミング_PHP</title>
	<atom:link href="http://zaru.tofu-kun.org/category/%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%9f%e3%83%b3%e3%82%b0_php/feed/" rel="self" type="application/rss+xml" />
	<link>http://zaru.tofu-kun.org</link>
	<description>Web系のこととかー。</description>
	<lastBuildDate>Tue, 11 May 2010 01:57:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://zaru.tofu-kun.org/category/%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%9f%e3%83%b3%e3%82%b0_php/feed/" />
		<item>
		<title>PHPでHTMLをパースする</title>
		<link>http://zaru.tofu-kun.org/2010/04/15/php%e3%81%a7html%e3%82%92%e3%83%91%e3%83%bc%e3%82%b9%e3%81%99%e3%82%8b/</link>
		<comments>http://zaru.tofu-kun.org/2010/04/15/php%e3%81%a7html%e3%82%92%e3%83%91%e3%83%bc%e3%82%b9%e3%81%99%e3%82%8b/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 10:09:10 +0000</pubDate>
		<dc:creator>zaru</dc:creator>
				<category><![CDATA[プログラミング_PHP]]></category>

		<guid isPermaLink="false">http://zaru.tofu-kun.org/?p=174</guid>
		<description><![CDATA[XMLじゃなくて、HTMLを解析したい…。しかもPHPで。
XMLだと、simplexml_load_file()っていう便利な関数がPHP5からあるけれど、HTMLはタグを取り除くぐらいしかない…というわけで、探してみたらHTMLをパースするライブラリがあった。
その名もまんまな、PHP Simple HTML DOM Parser。

PHP Simple HTML DOM Parser

使い方

include('simplehtmldom/simple_html_dom.php');
$html = &#60;&#60;&#60;EOM
&#60;ul&#62;
    &#60;li&#62;aaa&#60;/li&#62;
    &#60;li&#62;bbb&#60;/li&#62;
    &#60;li&#62;ccc&#60;/li&#62;
&#60;/ul&#62;
EOM;
$data = str_get_html($html);
foreach($data-&#62;find('li') as $element){
    echo $element-&#62;plaintext;
}
#=&#62;aaabbbccc

//URLやフィある名を指定して取得することも出来。
//その際は、 str_get_html() ではなく file_get_html() 。
$html = file_get_html('http://www.yahoo.co.jp/');

簡単な使い方は上記の通り。サイトにも書いてある通り、jQueryチックな使い方ができるので、以下のようなこともできる。

include('simplehtmldom/simple_html_dom.php');
$html = &#60;&#60;&#60;EOM
&#60;ul&#62;
    &#60;li&#62;aaa&#60;/li&#62;
    &#60;li id=&#34;hoge&#34;&#62;bbb&#60;/li&#62;
    &#60;li&#62;ccc&#60;strong&#62;ddd&#60;/strong&#62;&#60;/li&#62;
&#60;/ul&#62;
EOM;
$data = str_get_html($html);
foreach($data-&#62;find('#hoge') [...]]]></description>
			<content:encoded><![CDATA[<h3>XMLじゃなくて、HTMLを解析したい…。しかもPHPで。</h3>
<p>XMLだと、simplexml_load_file()っていう便利な関数がPHP5からあるけれど、HTMLはタグを取り除くぐらいしかない…というわけで、探してみたらHTMLをパースするライブラリがあった。</p>
<p>その名もまんまな、PHP Simple HTML DOM Parser。</p>
<ul>
<li><a href="http://simplehtmldom.sourceforge.net/" target="_blank">PHP Simple HTML DOM Parser</a></li>
</ul>
<h3>使い方</h3>
<pre class="brush: php;">
include('simplehtmldom/simple_html_dom.php');
$html = &lt;&lt;&lt;EOM
&lt;ul&gt;
    &lt;li&gt;aaa&lt;/li&gt;
    &lt;li&gt;bbb&lt;/li&gt;
    &lt;li&gt;ccc&lt;/li&gt;
&lt;/ul&gt;
EOM;
$data = str_get_html($html);
foreach($data-&gt;find('li') as $element){
    echo $element-&gt;plaintext;
}
#=&gt;aaabbbccc

//URLやフィある名を指定して取得することも出来。
//その際は、 str_get_html() ではなく file_get_html() 。
$html = file_get_html('http://www.yahoo.co.jp/');
</pre>
<p>簡単な使い方は上記の通り。サイトにも書いてある通り、jQueryチックな使い方ができるので、以下のようなこともできる。</p>
<pre class="brush: php;">
include('simplehtmldom/simple_html_dom.php');
$html = &lt;&lt;&lt;EOM
&lt;ul&gt;
    &lt;li&gt;aaa&lt;/li&gt;
    &lt;li id=&quot;hoge&quot;&gt;bbb&lt;/li&gt;
    &lt;li&gt;ccc&lt;strong&gt;ddd&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
EOM;
$data = str_get_html($html);
foreach($data-&gt;find('#hoge') as $element){
    echo $element-&gt;plaintext;
}
#=&gt;bbb
foreach($data-&gt;find('li strong') as $element){
    echo $element-&gt;plaintext;
}
#=&gt;ddd
echo $data-&gt;find('li',1)-&gt;id;
#=&gt;hoge
</pre>
<p>データを抜き出すだけではなく、HTMLを書き換えることもできる。</p>
<pre class="brush: php;">
    $html = &lt;&lt;&lt;EOM
&lt;ul&gt;
    &lt;li&gt;aaa&lt;/li&gt;
    &lt;li&gt;bbb&lt;/li&gt;
    &lt;li&gt;ccc&lt;/li&gt;
&lt;/ul&gt;
EOM;

    $data = str_get_html($html);
    $data-&gt;find('ul',0)-&gt;class = 'hoge';
    $data-&gt;find('li',2)-&gt;innertext = 'piyo';
    echo $data;
#=&gt;&lt;ul class=&quot;hoge&quot;&gt;
#=&gt; &lt;li&gt;aaa&lt;/li&gt;
#=&gt; &lt;li&gt;bbb&lt;/li&gt;
#=&gt; &lt;li&gt;piyo&lt;/li&gt;
#=&gt;&lt;/ul&gt;
</pre>
<p>非常に便利なので、色々と使い道がありそう。</p>
<img src="http://zaru.tofu-kun.org/?ak_action=api_record_view&id=174&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://zaru.tofu-kun.org/2010/04/15/php%e3%81%a7html%e3%82%92%e3%83%91%e3%83%bc%e3%82%b9%e3%81%99%e3%82%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://zaru.tofu-kun.org/2010/04/15/php%e3%81%a7html%e3%82%92%e3%83%91%e3%83%bc%e3%82%b9%e3%81%99%e3%82%8b/" />
	</item>
		<item>
		<title>PHPで複数の一括置換 str_replace</title>
		<link>http://zaru.tofu-kun.org/2010/04/13/php%e3%81%a7%e8%a4%87%e6%95%b0%e3%81%ae%e4%b8%80%e6%8b%ac%e7%bd%ae%e6%8f%9b-str_replace/</link>
		<comments>http://zaru.tofu-kun.org/2010/04/13/php%e3%81%a7%e8%a4%87%e6%95%b0%e3%81%ae%e4%b8%80%e6%8b%ac%e7%bd%ae%e6%8f%9b-str_replace/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 11:03:56 +0000</pubDate>
		<dc:creator>zaru</dc:creator>
				<category><![CDATA[プログラミング_PHP]]></category>

		<guid isPermaLink="false">http://zaru.tofu-kun.org/?p=170</guid>
		<description><![CDATA[PHPで複数の一括置換がしたい…
こんなお悩み、普通のPHPerなら悩む必要ないよね。あーあ、なんで今まで気がつかなかったんだろう。アホすぎる。普通にstr_replace() で実現可能じゃないか。置換対象に配列指定可能。

$str = 'iPad欲しい。Xperia欲しい。お金欲しい。';
$search = array('iPad','Xperia','お金');
echo str_replace($search,'彼女',$str);
#=&#62; 彼女欲しい。彼女欲しい。彼女欲しい。


$str = 'iPad欲しい。Xperia欲しい。お金欲しい。';
$search = array('iPad','Xperia','お金');
$replace = array('あれも','これも','もっともっと');
echo str_replace($search,$replace,$str);
#=&#62; あれも欲しい。これも欲しい。もっともっと欲しい。

置換する順序
配列で複数指定した場合の置換する順序は、配列の一番上から。つまり左から右へ。

$str = 'a';
$search = array('a','b','c');
$replace = array('b','c','d');
echo str_replace($search,$replace,$str);
#=&#62; d

]]></description>
			<content:encoded><![CDATA[<h3>PHPで複数の一括置換がしたい…</h3>
<p>こんなお悩み、普通のPHPerなら悩む必要ないよね。あーあ、なんで今まで気がつかなかったんだろう。アホすぎる。普通にstr_replace() で実現可能じゃないか。置換対象に配列指定可能。</p>
<pre class="brush: php;">
$str = 'iPad欲しい。Xperia欲しい。お金欲しい。';
$search = array('iPad','Xperia','お金');
echo str_replace($search,'彼女',$str);
#=&gt; 彼女欲しい。彼女欲しい。彼女欲しい。
</pre>
<pre class="brush: php;">
$str = 'iPad欲しい。Xperia欲しい。お金欲しい。';
$search = array('iPad','Xperia','お金');
$replace = array('あれも','これも','もっともっと');
echo str_replace($search,$replace,$str);
#=&gt; あれも欲しい。これも欲しい。もっともっと欲しい。
</pre>
<h3>置換する順序</h3>
<p>配列で複数指定した場合の置換する順序は、配列の一番上から。つまり左から右へ。</p>
<pre class="brush: php;">
$str = 'a';
$search = array('a','b','c');
$replace = array('b','c','d');
echo str_replace($search,$replace,$str);
#=&gt; d
</pre>
<img src="http://zaru.tofu-kun.org/?ak_action=api_record_view&id=170&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://zaru.tofu-kun.org/2010/04/13/php%e3%81%a7%e8%a4%87%e6%95%b0%e3%81%ae%e4%b8%80%e6%8b%ac%e7%bd%ae%e6%8f%9b-str_replace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://zaru.tofu-kun.org/2010/04/13/php%e3%81%a7%e8%a4%87%e6%95%b0%e3%81%ae%e4%b8%80%e6%8b%ac%e7%bd%ae%e6%8f%9b-str_replace/" />
	</item>
		<item>
		<title>CakePHPをPHP5.3で使うとエラーになる時の対処法</title>
		<link>http://zaru.tofu-kun.org/2010/03/10/cakephp%e3%82%92php5-3%e3%81%a7%e4%bd%bf%e3%81%86%e3%81%a8%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%ab%e3%81%aa%e3%82%8b%e6%99%82%e3%81%ae%e5%af%be%e5%87%a6%e6%b3%95/</link>
		<comments>http://zaru.tofu-kun.org/2010/03/10/cakephp%e3%82%92php5-3%e3%81%a7%e4%bd%bf%e3%81%86%e3%81%a8%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%ab%e3%81%aa%e3%82%8b%e6%99%82%e3%81%ae%e5%af%be%e5%87%a6%e6%b3%95/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 09:03:02 +0000</pubDate>
		<dc:creator>zaru</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[プログラミング_PHP]]></category>

		<guid isPermaLink="false">http://zaru.tofu-kun.org/?p=154</guid>
		<description><![CDATA[PHP5.3だとエラーが色々と…
PHP5.1からPHP5.3へアップデートをすると、CakePHP1.2においてエラーが出てしまったので、その場合の対処法。
Deprecatedエラーが出る
Deprecated: Assigning the return value of new by reference is deprecated in云々なエラー。

cake/libs/configure.php (295行目付近）


if (isset($config['debug'])) {
	if ($_this-&#62;debug) {
		error_reporting(E_ALL);
		//下記追加
		if (error_reporting() &#62; 6143) {
			error_reporting(E_ALL &#38; ~E_DEPRECATED);
		}
		//ここまで

strtotime()でタイムゾーンのエラーが出る
Warning: strtotime() [http://php.net/function.strtotime]: It is not safe to rely on the system’s timezone settings.というエラー。

/etc/php.ini


[Date]
; Defines the default timezone used by the date functions
;date.timezone =
date.timezone =Asia/Tokyo

と、タイムゾーンを設定してやれば大丈夫。
]]></description>
			<content:encoded><![CDATA[<h3>PHP5.3だとエラーが色々と…</h3>
<p>PHP5.1からPHP5.3へアップデートをすると、CakePHP1.2においてエラーが出てしまったので、その場合の対処法。</p>
<h3>Deprecatedエラーが出る</h3>
<p>Deprecated: Assigning the return value of new by reference is deprecated in云々なエラー。</p>
<ul>
<li>cake/libs/configure.php (295行目付近）</li>
</ul>
<pre class="brush: php;">
if (isset($config['debug'])) {
	if ($_this-&gt;debug) {
		error_reporting(E_ALL);
		//下記追加
		if (error_reporting() &gt; 6143) {
			error_reporting(E_ALL &amp; ~E_DEPRECATED);
		}
		//ここまで
</pre>
<h3>strtotime()でタイムゾーンのエラーが出る</h3>
<p>Warning: strtotime() [http://php.net/function.strtotime]: It is not safe to rely on the system’s timezone settings.というエラー。</p>
<ul>
<li>/etc/php.ini</li>
</ul>
<pre class="brush: bash;">
[Date]
; Defines the default timezone used by the date functions
;date.timezone =
date.timezone =Asia/Tokyo
</pre>
<p>と、タイムゾーンを設定してやれば大丈夫。</p>
<img src="http://zaru.tofu-kun.org/?ak_action=api_record_view&id=154&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://zaru.tofu-kun.org/2010/03/10/cakephp%e3%82%92php5-3%e3%81%a7%e4%bd%bf%e3%81%86%e3%81%a8%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%ab%e3%81%aa%e3%82%8b%e6%99%82%e3%81%ae%e5%af%be%e5%87%a6%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://zaru.tofu-kun.org/2010/03/10/cakephp%e3%82%92php5-3%e3%81%a7%e4%bd%bf%e3%81%86%e3%81%a8%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%ab%e3%81%aa%e3%82%8b%e6%99%82%e3%81%ae%e5%af%be%e5%87%a6%e6%b3%95/" />
	</item>
	</channel>
</rss>
