Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting sth from <abc x="12">sth</abc>

Status
Not open for further replies.

1510982

Programmer
Feb 17, 2002
57
SI
is there a simple function that does this:

$string = '<abc x="12">sth</abc>';

$start_part = '<abc x="12">'; //left to searched value
$end_part = '</abc>'; //right to search value

$value = separate($string, $start_part, $end_part);

echo "value: " . $value;
---------
OUTPUT:
value: sth
---------
actually, i want to parse larger $string than in example, but i'm sure it illustrates the problem.

thank you for answers!
 
try regex or you could explode on the '>' character

Bastien

Cat, the other other white meat
 
Take a look at strip_tags()

If that doesn't work, how about preg_split?
(Someone else supply the regex, please)

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
If you just wish to remove all html, the strip_tags() is the right way to go, like chessbot suggested.

If you however wish to replace the tags with something else, or explode all the "words" inside the html tags, you need some pattern matching replacement.

I think you should tell us your goal, before we can tell you what we think might be good for you.

eg. What do you wish to do? Just remove the html?
If so, use strip_tags(), like chessbot said.

Olav Alexander Mjelde
Admin & Webmaster
 
hmm, thanks for posts. however sure i was to be exact, i'm not anymore.

let's say that $string is something like this:

<title>nasegage</title><table><tr><td>asrgasegaef</td><tr>blalalabargagregaseg</table><row></row>asregasegage<yeah>asegeagaseg</year>aersgasegahthsragref<sth height="23">asegasegaeg</sth>asgaseggathsgasgasegasregasgzd56wszhgarharsagea<b><abc x="12">sth</abc></b>asrgarsghargaefgasegaseg<title>nasegage</title><table><tr><td>asrgasegaef</td><tr>blalalabargagregaseg</table>

i want to extract something between left and right side that constantly changes position in this $string and it's not neccessarily tag. that could be even ABCDsthEFGH with $value = separate($string, "ABCD", "EFGH") if want to get sth from this string. it doesn't have to to with tags at all.

i hope you now understand better what i'm up to ;)

that's for further suggestions.
 
Hi,
Are you looking for something like this:

Code:
$str = "ABCDsthEFGH" ;
$start = "ABCD" ;
$end = "EFGH" ;

if ( preg_match("/$start([\w]+)$end/",$str,$arr ) ) {
   echo $arr[1] ;
}

Output:sth



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
spookie: you are the 1337 regexp-man.

I dont know how this script will be used, but I guess it will be bombed with arrays?

If not, it's simply to wrap this in a function:
Code:
<?php
$str = "ABCDsthEFGH<title>nasegage</title><table><tr><td>asrgasegaef</td><tr>blalalabargagregaseg</table>" ;
$start = "ABCD" ;
$end = "EFGH" ;
echo removetags($start, $end, $str);

function removetags($start, $end, $str)
  {
    if (isset($start) && isset($end) && isset($str)) {
      if ( preg_match("/$start([\w]+)$end/",$str,$arr ) ) {
         return $arr[1] ;
      }
    } // end if values are set
    else {
      return "missing input!";
    }
  } // end function removetags()

?>

Olav Alexander Mjelde
Admin & Webmaster
 
needless to say, but I'll say it anyway:

remember the case else, in case regexp not matched.. (pattern not found).
Simply return "pattern not found" or something.

You might also want to return codes, 404 = missing, etc.

Olav Alexander Mjelde
Admin & Webmaster
 
I see one minor issue with spookie's regular expression. It will fail if there is white space character(s) between the opening tag and the ending tag, or $start and $end. The subject might not be covered by \w.
Another approach:
Use explode():
Code:
<?php
$str = "<html><head><!-- whatever there is here --></head><body attribute="something">ABCDsthEFGH<title>nasegage</title><table><tr><td>asrgasegaef</td><tr>blalalabargagregaseg</table>" ;
$start = "ABCD" ;
$end = "EFGH" ;
# let's make sure the $start is in there
if (strstr($str,$start)){
   # explode the subject on the start
   $tempArr = explode($start,$str);
   # offset 1 has the part to the right of $start
   # explode on $end
   $tempArr = explode($end,$tempArr[1]);
   # your extracted content:
   $content  = $tempArr[0];
} else {
  echo("No start identifier found");
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top