Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Get contents from text file

Posted on 2006-07-21
3
181 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:gstalin
  • 2
3 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 17156454
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-
0
 
LVL 2

Expert Comment

by:arun80_inin
ID: 17159597
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;
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 17159599
Then you can better use:

$title1 = preg_match('title1="(.*?)"', $str, $m) ? $m[1] : '';
$title2 = preg_match('title2="(.*?)"', $str, $m) ? $m[1] : '';

-r-
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question