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.

greg0ire
23.3k17 gold badges76 silver badges104 bronze badges
asked Aug 22, 2010 at 18:30

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
?>
answered Aug 22, 2010 at 18:36
1
 $string = "http://www.twitpic.com/f1462i" ;
 $id = substr($string,strpos($string, 'twitpic.com')+strlen('twitpic.com')+1,6) ;
 echo $id ;
answered Aug 23, 2010 at 8:27
0
preg_match("@twitpic\.com/(\w{6})@", "The url is http://www.twitpic.com/f1462i. Enjoy.", $m);
$id = $m[1];
answered Aug 22, 2010 at 18:34
3
  • 1
    -1: Do not use regexes when you can avoid them, they are a performance killer. Commented Aug 22, 2010 at 18:37
  • @greg0ire: Why are you downvoting the answer when it was the OP who tagged the question "regex"? Commented Aug 23, 2010 at 4:08
  • @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. Commented Aug 23, 2010 at 8:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.