0

What's the easiest way to grab a 6-character id from a string?

The id will always be after www.twitpic.com/ and will always be 6 characters.

e.g., $string = 'The url is http://www.twitpic.com/f1462i.  Enjoy.';
      $id = 'f1462i';

Thanks.

3 Answers 3

3

Here you go. Complete working code without regex :

<?php
$string = 'The url is http://www.twitpic.com/f1462i.  Enjoy.';
$id = substr($string, strpos($string, 'http://www.twitpic.com/')+23, 6);
echo $id;   //output: f1462i
?>
Sign up to request clarification or add additional context in comments.

Comments

1
  $string = "http://www.twitpic.com/f1462i" ;
  $id = substr($string,strpos($string, 'twitpic.com')+strlen('twitpic.com')+1,6) ;
  echo $id ;

Comments

0
preg_match("@twitpic\.com/(\w{6})@", "The url is http://www.twitpic.com/f1462i.  Enjoy.", $m);
$id = $m[1];

3 Comments

-1: Do not use regexes when you can avoid them, they are a performance killer.
@greg0ire: Why are you downvoting the answer when it was the OP who tagged the question "regex"?
@Alan Moore: because I didn't read the tags... I would cancel this if I could but it seems to be too late. I retag the question, regexes have nothing to do with this.

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.