views:

75

answers:

8

I have the following XML:

<id>tag:search.twitter.com,2005:22204349686</id>

How can i write everything after the second colon to a variable?

E.g. 22204349686

+3  A: 
if(preg_match('#<id>.*?:.*?:(.*?)</id>#',$input,$m)) {
 $num = $m[1];
}
codaddict
+2  A: 

When you already have just the tags content in a variable $str, you could use explode to get everything from the second : on:

list(,,$rest) = explode(':', $str, 3);
Gumbo
+1  A: 

$var = preg_replace('/^([^:]+:){2}/', '', 'tag:search.twitter.com,2005:22204349686');

I am assuming you already have the string without the <id> bits.

Otherwise, for SimpleXML: $var = preg_replace('/^([^:]+:){2}/', '', "{$yourXml->id}");

janmoesen
Correct answer because you read my mind!
danit
A: 

I suppose you have in a variable ($str) the content of id tag.

// get last occurence of colon
$pos = strrpos($str, ":");

if ($pos !== false) {
  // get substring of $str from position $pos to the end of $str 
  $result = substr($str, $pos); 
} else {
  $result = null;
}
MartyIX
And what is the reason for the downvote?
MartyIX
A: 

Hi,

use explode and strip_tags

list(,,$id) = explode( ':', strip_tags( $input ), 3 );

Cheers,
haggi

haggi
A: 
function between($t1,$t2,$page) {
    $p1=stripos($page,$t1);
    if($p1!==false) {
        $p2=stripos($page,$t2,$p1+strlen($t1));
    } else {
        return false;
    }
    return substr($page,$p1+strlen($t1),$p2-$p1-strlen($t1));
}

$x='<id>tag:search.twitter.com,2005:22204349686</id>';
$text=between(',','<',$x);
if($text!==false) {
   //got some text..
}
vlad b.
A: 

First, parse the XML with an XML parser. Find the text content of the node in question (tag:search.twitter.com,2005:22204349686). Then, write a relevant regex, e.g.

<?php
$str = 'tag:search.twitter.com,2005:22204349686';
preg_match('#^([^:]+):([^,]+),([0-9]+):([0-9]+)#', $str, $matches);
var_dump($matches);
Frank Farmer
A: 

Regex seems to me inappropriate for such a simple matching.

If you dont have the ID tags around the string, you can simply do

echo trim(strrchr($xml, ':'), ':');

If they are around, you can use

$xml = '<id>tag:search.twitter.com,2005:22204349686</id>';
echo filter_var(strrchr($xml, ':'), FILTER_SANITIZE_NUMBER_INT);
// 22204349686

The strrchr part returns :22204349686</id> and the filter_var part strips everything that's not a number.

Gordon