PHP array values issue with two if statements
Get the solution ↓↓↓Solution:
I recommend that you pull the entire contents of your input file into a string (usingfile_get_contents
) rather than work line-by-line because your targeted data arrives in pairs of lines.
My supplied pattern uses some very general negated character classes and techniques to isolate the substrings in your sample data. If this pattern does not work for your actual project, you will need to provide a more realistic set of input data.
Pattern Demo <-- this provides the pattern breakdown
Code: (Demo)
$contents = file_get_contents($file);
if (!preg_match_all('~^([^:>]+):\s(\S+)[^:]+:\s(\S+)\s/\s(\S+)\s/\s(\S+)\s*$~m', $contents, $matches, PREG_SET_ORDER)) {
echo "Pattern Failed";
} else {
echo "<table>";
foreach ($matches as $i => $data) {
echo "<tr>";
echo "<td>Test case " , ++$i , "</td>";
echo "<td>{$data[1]}</td>";
echo "<td>{$data[3]}</td>";
echo "<td>{$data[4]}</td>";
echo "<td>{$data[5]}</td>";
echo "<td>{$data[2]}</td>";
echo "</tr>";
}
echo "</table>";
}
Output:
<table>
<tr>
<td>Test case 1</td>
<td>abc test1 for test1</td>
<td>x1</td>
<td>y2</td>
<td>z3</td>
<td>xxx</td>
</tr>
<tr>
<td>Test case 2</td>
<td>abc test2 for test2</td>
<td>x1</td>
<td>y2</td>
<td>z3</td>
<td>xxx</td>
</tr>
<tr>
<td>Test case 3</td>
<td>abc test3 for test3</td>
<td>x1</td>
<td>y2</td>
<td>z3</td>
<td>xxx</td>
</tr>
</table>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: illegal string offset
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.