1

this is my first post. I already some days I tried but did not succeed.

Case: I want remove some of the words in the link from this link "http://www.amazon.com/Mindfulness-Be-mindful-Live-moment/dp/0857084445/ref=sr_1_1?s=books&ie=UTF8&qid=1443534574&sr=1-1&keywords=Mindfulness+Be+Mindful+Live+In+The+Moment"

this is my code

<?php 
$s = 'http://www.amazon.com/Mindfulness-Be-mindful-Live-moment/dp/0857084445/ref=sr_1_1?s=books&ie=UTF8&qid=1443534574&sr=1-1&keywords=Mindfulness+Be+Mindful+Live+In+The+Moment';
preg_match("/http:/(.*)//", $s, $results);
echo $results[0];
?>

No for substr function cause the link is dynamic. I want keep "0857084445" using php regex. Your solution really helped me. Thank you

6
  • 1
    What's the exact output that do you want from the script? http://www.amazon.com/Mindfulness-Be-mindful-Live-moment/dp/0857084445/ or only 0857084445 ? Commented Sep 29, 2015 at 16:05
  • i would like to see answer of this question Commented Sep 29, 2015 at 16:08
  • let me know if this part always remain same ? http://www.amazon.com/Mindfulness-Be-mindful-Live-moment/dp/ Commented Sep 29, 2015 at 16:08
  • Is the number always in the same place, would using explode() on / and accessing the relevant key do the job? Commented Sep 29, 2015 at 16:11
  • no but always using this pattern amazon.com/xxxx/dp Commented Sep 29, 2015 at 16:11

2 Answers 2

2

You should use this regex #/(\d+)/#i that matches every digits between 2 /.

Not only your regexp is wrong, but the output of reults too. Since $results[0] contains the matched string. $results[1] the first caputured group and so on..

Here's the corrected code

<?php 
$s = 'http://www.amazon.com/Mindfulness-Be-mindful-Live-moment/dp/0857084445/ref=sr_1_1?s=books&ie=UTF8&qid=1443534574&sr=1-1&keywords=Mindfulness+Be+Mindful+Live+In+The+Moment';
preg_match('#/(\d+)/#', $s, $results);
echo $results[1];
?>
Sign up to request clarification or add additional context in comments.

6 Comments

And it will output 0857084445 but if links or place of number changed then it will give you wrong output
what are the hashtags for ?
anyway it wont work if you will have a digit in text before this number, you should use '/dp\/(\d+)\//' instead
the # sign is a regex delimiter, the same of /. But with # you should't escape /, thus the regex is easy to read. Btw it will work with every digits placed between two / (try it). It's not necessary to change the regexp.
You are right. But why do you use case insensitive mode?
|
-1

If your question pertains to display purposes only, it would be a lot easier to do something like this since regex would not be needed.

<?php
$s = 'http://www.amazon.com/Mindfulness-Be-mindful-Live-moment/dp/0857084445/ref=sr_1_1?s=books&ie=UTF8&qid=1443534574&sr=1-1&keywords=Mindfulness+Be+Mindful+Live+In+The+Moment';
?> <a href="<?php echo $s?>"> Whatever you want here</a>

Edit: I would have asked if it was for display only in the comments section, if I had 50 rep.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.