Solved

Get contents from text file

Posted on 2006-07-21
3
184 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
[X]
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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
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.

730 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