在 PHP 中使用 preg_match 时,避免将结果的每个值放入每个数组中
Avoid getting each value of the result into each array when used preg_match in PHP
我是 PHP 的新手,正在尝试使用 preg_match
从 HTML 文件中获取两个标签之间的数据,
好的,我得到了数据,但这里的问题是获得的每个值都进入每个数组,就像这样...
Array
(
[0] => Bachelors degree
)
Array
(
[0] => Johny
)
Array
(
[0] => bsc
)
Array
(
[0] => java
)
Array
(
[0] => 1year
)
这是我为获取数据而编写的代码...
$data=file_get_contents($dest); //$dest is the file name
$convert=explode("\n",$data);
foreach($convert as $key=>$value)
{
$output1=preg_match('/^.*<span style="font-family:Verdana; font-size:18px; color:#555555;">.*$/',$value,$match1);
if($output1)
{
print_r($match1);
}
}
实际上我现在想要的是,我想将所有数据放在一个数组中..即
array(
[0] => Bachelors degree
[1] => Johny
[2] => bsc
[3] => java
[4] => 1year
)
我用谷歌搜索并在 Whosebug 中搜索了解决方案,但找不到任何...有人可以帮助我/给我建议如何做吗?
谢谢..
更新:
我假设,您的名字在 span 标签中。这是一个示例数据:
//$data = file_get_contents($dest); //$dest is the file name
$data = '<span style="font-family:Verdana; font-size:18px; color:#555555;">Bachelors degree</span>' . "\n";
$data .= '<span style="font-family:Verdana; font-size:18px; color:#555555;">Johny</span>' . "\n";
$data .= '<span style="font-family:Verdana; font-size:18px; color:#555555;">bsc</span>' . "\n";
$data .= '<span style="font-family:Verdana; font-size:18px; color:#555555;">Java</span>' . "\n";
$data .= '<span style="font-family:Verdana; font-size:18px; color:#555555;">1 year</span>' . "\n";
$convert = explode("\n", $data);
$newArray = array();
foreach ($convert as $key => $value) {
$output1 = preg_match('/^.*<span style="font-family:Verdana; font-size:18px; color:#555555;">(.*)<\/span>$/', $value, $match1);
if ($output1) {
$newArray[] = $match1[1];
}
}
var_dump($newArray);
输出为:
array (size=5)
0 => string 'Bachelors degree' (length=16)
1 => string 'Johny' (length=5)
2 => string 'bsc' (length=3)
3 => string 'Java' (length=4)
4 => string '1 year' (length=6)
更新 2:
$value = '<p style="color:#555555;"> '."\n".' Html, css, photoshop, javascript,jquery, dreamwaver.. '."\n".' </p>';
preg_match('/<p style="color:#555555;">\s(.*)<\/p>/sm',$value,$match2);
var_dump($match2);
输出:
array (size=2)
0 => string '<p style="color:#555555;">
Html, css, photoshop, javascript,jquery, dreamwaver..
</p>' (length=89)
1 => string '
Html, css, photoshop, javascript,jquery, dreamwaver..
' (length=58)
修饰符 s
是你的朋友。
我是 PHP 的新手,正在尝试使用 preg_match
从 HTML 文件中获取两个标签之间的数据,
好的,我得到了数据,但这里的问题是获得的每个值都进入每个数组,就像这样...
Array
(
[0] => Bachelors degree
)
Array
(
[0] => Johny
)
Array
(
[0] => bsc
)
Array
(
[0] => java
)
Array
(
[0] => 1year
)
这是我为获取数据而编写的代码...
$data=file_get_contents($dest); //$dest is the file name
$convert=explode("\n",$data);
foreach($convert as $key=>$value)
{
$output1=preg_match('/^.*<span style="font-family:Verdana; font-size:18px; color:#555555;">.*$/',$value,$match1);
if($output1)
{
print_r($match1);
}
}
实际上我现在想要的是,我想将所有数据放在一个数组中..即
array(
[0] => Bachelors degree
[1] => Johny
[2] => bsc
[3] => java
[4] => 1year
)
我用谷歌搜索并在 Whosebug 中搜索了解决方案,但找不到任何...有人可以帮助我/给我建议如何做吗? 谢谢..
更新:
我假设,您的名字在 span 标签中。这是一个示例数据:
//$data = file_get_contents($dest); //$dest is the file name
$data = '<span style="font-family:Verdana; font-size:18px; color:#555555;">Bachelors degree</span>' . "\n";
$data .= '<span style="font-family:Verdana; font-size:18px; color:#555555;">Johny</span>' . "\n";
$data .= '<span style="font-family:Verdana; font-size:18px; color:#555555;">bsc</span>' . "\n";
$data .= '<span style="font-family:Verdana; font-size:18px; color:#555555;">Java</span>' . "\n";
$data .= '<span style="font-family:Verdana; font-size:18px; color:#555555;">1 year</span>' . "\n";
$convert = explode("\n", $data);
$newArray = array();
foreach ($convert as $key => $value) {
$output1 = preg_match('/^.*<span style="font-family:Verdana; font-size:18px; color:#555555;">(.*)<\/span>$/', $value, $match1);
if ($output1) {
$newArray[] = $match1[1];
}
}
var_dump($newArray);
输出为:
array (size=5)
0 => string 'Bachelors degree' (length=16)
1 => string 'Johny' (length=5)
2 => string 'bsc' (length=3)
3 => string 'Java' (length=4)
4 => string '1 year' (length=6)
更新 2:
$value = '<p style="color:#555555;"> '."\n".' Html, css, photoshop, javascript,jquery, dreamwaver.. '."\n".' </p>';
preg_match('/<p style="color:#555555;">\s(.*)<\/p>/sm',$value,$match2);
var_dump($match2);
输出:
array (size=2)
0 => string '<p style="color:#555555;">
Html, css, photoshop, javascript,jquery, dreamwaver..
</p>' (length=89)
1 => string '
Html, css, photoshop, javascript,jquery, dreamwaver..
' (length=58)
修饰符 s
是你的朋友。