4

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

8 Answers 8

3
if(preg_match('#<id>.*?:.*?:(.*?)</id>#',$input,$m)) {
 $num = $m[1];
}
Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

1

$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}");

Comments

0

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);

Comments

0

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;
}

Comments

0

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.

Comments

0

Use explode and strip_tags:

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

Comments

-2
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..
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.