Link to home
Start Free TrialLog in
Avatar of gstalin
gstalin

asked on

Get contents from text file

Hi Guys,

I have the text file which have the contents given below.

"<text title1="Training Area" title2="Test">
<p align="left"><b>HTML</b> Content for the training.<br>
This area will be filled with HTML source code to be displayed into the page.
</text>"

I want to get the contents only the attribute values of 'title1' and 'title2' in the text tag.

How to get this? Please provide me the solution for this.
Avatar of Roonaan
Roonaan
Flag of Netherlands image

If you want to use xml parser, then I would suggest using xml_unserialize from Keith Devens.

If you want to use preg_match you can use:
$title1 = '';
$title2 = '';
if(preg_match_all('/(title\d+)="(.*?)"/', $text, $results)) {
  foreach($results[0] as $key => $value) {
      if($results[1][$key] == 'title1') $title1 = $results[2][$key];
      if($results[1][$key] == 'title2') $title2 = $results[2][$key];
  }
}

-r-
Avatar of arun80_inin
arun80_inin

Very bad solution. If you want try this

$title2 = '';
$arr=explode("title1=\"",$str);
$arr=explode("\"",$arr[1]);
$title1=$arr[0];

$arr=explode("title2=\"",$str);
$arr=explode("\"",$arr[1]);
$title2=$arr[0];
echo "<br>title1=".$title1;
echo "<br>title2=".$title2;
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial