Good Morning.
In my previous post I was asking some help in order
to be able to extract only strings surrounded by
particular separators/tags in a file.
Because I have found the solution after searching
until late this night, and although I am aware this
is a beginner's level tip, I thought it would be nice
to post this code which could help other beginners like
me.
So, what this code is suppose to do?
(1) Let's say we have the following text found in a file called test.txt:
-----------<b>a</b>---------<b>b</b>-----------<b>c</b>----------<b>d</b>-----------
-----------<b>e</b>---------<b>f</b>-----
-----------<b>g</b>---------<b>h</b>-----------<b>i</b>----------
-----------<b>j</b>---------<b>k</b>-----------<b>l</b>----------<b>m</b>-----------
-----------<b>n</b>-----
(2) When using the following code:
$fp = fopen("test.txt", "r"
$content = "";
$lines_num = 1;
while (!feof($fp)) {
$line = fgets($fp, 100);
$prep = explode("<b>",$line);
$occur = sizeof($prep) - 1;
$content .= "Line $lines_num :<br>";
for($i=1; $i <= $occur; $i++ ) {
$xtracted_array = explode("</b>", $prep[$i]);
$content .= "Extracted String Number $i: $xtracted_array[0]<br>";
}
$lines_num++;
}
echo"
$content
";
(3) You get this result:
Line 1 :
Extracted String Number 1: a
Extracted String Number 2: b
Extracted String Number 3: c
Extracted String Number 4: d
Line 2 :
Extracted String Number 1: e
Extracted String Number 2: f
Line 3 :
Extracted String Number 1: g
Extracted String Number 2: h
Extracted String Number 3: i
Line 4 :
Extracted String Number 1: j
Extracted String Number 2: k
Extracted String Number 3: l
Extracted String Number 4: m
Line 5 :
Extracted String Number 1: n
That's all.
I'm sure there may be an easier way to get the same
thing done though.
Have a good day, and thanks to those who helped me.
In my previous post I was asking some help in order
to be able to extract only strings surrounded by
particular separators/tags in a file.
Because I have found the solution after searching
until late this night, and although I am aware this
is a beginner's level tip, I thought it would be nice
to post this code which could help other beginners like
me.
So, what this code is suppose to do?
(1) Let's say we have the following text found in a file called test.txt:
-----------<b>a</b>---------<b>b</b>-----------<b>c</b>----------<b>d</b>-----------
-----------<b>e</b>---------<b>f</b>-----
-----------<b>g</b>---------<b>h</b>-----------<b>i</b>----------
-----------<b>j</b>---------<b>k</b>-----------<b>l</b>----------<b>m</b>-----------
-----------<b>n</b>-----
(2) When using the following code:
$fp = fopen("test.txt", "r"
$content = "";
$lines_num = 1;
while (!feof($fp)) {
$line = fgets($fp, 100);
$prep = explode("<b>",$line);
$occur = sizeof($prep) - 1;
$content .= "Line $lines_num :<br>";
for($i=1; $i <= $occur; $i++ ) {
$xtracted_array = explode("</b>", $prep[$i]);
$content .= "Extracted String Number $i: $xtracted_array[0]<br>";
}
$lines_num++;
}
echo"
$content
";
(3) You get this result:
Line 1 :
Extracted String Number 1: a
Extracted String Number 2: b
Extracted String Number 3: c
Extracted String Number 4: d
Line 2 :
Extracted String Number 1: e
Extracted String Number 2: f
Line 3 :
Extracted String Number 1: g
Extracted String Number 2: h
Extracted String Number 3: i
Line 4 :
Extracted String Number 1: j
Extracted String Number 2: k
Extracted String Number 3: l
Extracted String Number 4: m
Line 5 :
Extracted String Number 1: n
That's all.
I'm sure there may be an easier way to get the same
thing done though.
Have a good day, and thanks to those who helped me.