1

I'm just new with php and recently I was try to make a use of preg_match_all().

source: http//:test.com

 <tr>
    <td class="build">
    <a id="viewBuild:TEST-TESTAUTO">Sample - Regression 01</a>
    </td>
    <td class="planKeySection Successful">
    <a id="statusSectionTEST-TESTAUTO" class="statusIcon">
    <span class="aui-icon aui-icon-small aui-iconfont-approve" title="build succeeded">
    </a>


<tr>
<td class="build">
<a id="viewBuild:TEST-TESTAUTO02">Sample - Regression 02</a>
</td>
<td class="planKeySection Successful">
<a id="statusSectionTEST-TESTAUTO02" class="statusIcon">
<span class="aui-icon aui-icon-small aui-iconfont-approve" title="build succeeded">
</a>

This code doesn't work:

$content = file_get_contents('http//:test.com');

if(preg_match_all( "'/<tr>[1]<span class=\"aui-icon aui-icon-small aui-iconfont-approve\"><\/tr>/isU'", $content)) 
    echo "done";
}else{
    echo "fail";
}

if(preg_match_all( "'/<tr>[2]<span class=\"aui-icon aui-icon-small aui-iconfont-approve\"><\/tr>/isU'", $content)) 
    echo "done";
}else{
    echo "fail";
}

as you can see above, I'm trying to get a certain element on above source and make a condition that if it's <span class="aui-icon aui-icon-small aui-iconfont-approve"> then it will echo "done" but all I get is "fail".

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Liyo
  • 19
  • 2
  • 7

2 Answers2

0
if(count(preg_match_all($pattern, $content))>0)

or

if(preg_match($pattern, $content))

What you also have to do is fix the URL: it has to be http://test.com not http//:test.com

Grunzwanzling
  • 443
  • 4
  • 14
  • ^sorry bout the URL, is just a made up. I was using a different one. Anyway I'll try the code above. Hope it works. Thanks – Liyo Mar 11 '17 at 14:10
  • I was wondering, can I also call this tags in just one preg_match? basically, I want to preg match, if id=statusSectionTEST-TESTAUTO02 and class="aui-icon aui-icon-small aui-iconfont-approve" then echo done, else fail. But not sure how im going to write it lol – Liyo Mar 11 '17 at 23:47
0

To match the first substring use:

~id="statusSectionTEST-TESTAUTO" class="statusIcon">\s+<span class="aui-icon aui-icon-small aui-iconfont-approve"~

To match the second substring use:

~id="statusSectionTEST-TESTAUTO02" class="statusIcon">\s+<span class="aui-icon aui-icon-small aui-iconfont-approve"~

The only adjust that I made to the literal string to create the pattern was \s+ so that white-space characters don't cause an undesired mismatch.

Here is a regex demo for the second pattern.

Code (PHP Demo):

$content=' <tr>
    <td class="build">
    <a id="viewBuild:TEST-TESTAUTO">Sample - Regression 01</a>
    </td>
    <td class="planKeySection Successful">
    <a id="statusSectionTEST-TESTAUTO" class="statusIcon">
    <span class="aui-icon aui-icon-small aui-iconfont-approve" title="build succeeded">
    </a>


<tr>
<td class="build">
<a id="viewBuild:TEST-TESTAUTO02">Sample - Regression 02</a>
</td>
<td class="planKeySection Successful">
<a id="statusSectionTEST-TESTAUTO02" class="statusIcon">
<span class="aui-icon aui-icon-small aui-iconfont-approve" title="build succeeded">
</a>';

$pattern='~id="statusSectionTEST-TESTAUTO02" class="statusIcon">\s+<span class="aui-icon aui-icon-small aui-iconfont-approve"~';

if(preg_match($pattern,$content)){
    echo "found";
}else{
    echo "not found"; 
}

After re-reading your question, if you just want to capture <span class="aui-icon aui-icon-small aui-iconfont-approve"

Then strpos() will be sufficient:

Code (PHP Demo):

$content=' <tr>
    <td class="build">
    <a id="viewBuild:TEST-TESTAUTO">Sample - Regression 01</a>
    </td>
    <td class="planKeySection Successful">
    <a id="statusSectionTEST-TESTAUTO" class="statusIcon">
    <span class="aui-icon aui-icon-small aui-iconfont-approve" title="build succeeded">
    </a>


<tr>
<td class="build">
<a id="viewBuild:TEST-TESTAUTO02">Sample - Regression 02</a>
</td>
<td class="planKeySection Successful">
<a id="statusSectionTEST-TESTAUTO02" class="statusIcon">
<span class="aui-icon aui-icon-small aui-iconfont-approve" title="build succeeded">
</a>';
$html ='<span class="aui-icon aui-icon-small aui-iconfont-approve"';

if(strpos($content,$html)!==false){
    echo "found";
}else{
    echo "not found"; 
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136