Substring js in php

Bartłomiej Giepard

enter code here I'm trying to get out ID from link:

www.imdb.com/title/tt5807628/   - >  tt5807628  

My code in javascript:

  var str = "www.imdb.com/title/tt5807628/";
  var n = str.search("e/tt");
  var res = str.substring(n+2, n+30);
  var ukos = res.search("/");
  var last = res.substring(0, ukos);

I would like to get the same effect in PHP, how to do it?

GrumpyCrouton

Based off my comment here, the following code will just give you the ID:

$id = explode("/", "www.imdb.com/title/tt5807628/")[2];

We use the explode(delimiter, string) function here to break the string at each of the slashes, which creates an index of strings that looks like this:

array (
    0 => "www.imdb.com"
    1 => "title"
    2 => "tt5707627"
)

So, as you can see array index 2 is our ID, so we select that after we break the string (which is the [2] at the end of the variable declaration), leaving us with a variable $id that contains just the ID from the link.


edit:

You could also use parse_url before the explode, just to ensure that you dont run into http(s):// if the link changes due to user input. - Keja

$id = explode("/", parse_url("www.imdb.com/title/tt5807628/", PHP_URL_PATH))[2];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

php cannot work with a substring

From Dev

PHP: capture a multiline substring

From Dev

Substring in php or in sql query

From Dev

Search substring in a PHP Array

From Dev

In PHP substring return �� these type of character?

From Dev

Replace substring in keys of an array in PHP

From Dev

Replace substring with an elaboration of $1 in PHP

From Dev

Check substring in string array in PHP

From Dev

php: extracting substring from a string

From Dev

In PHP substring return �� these type of character?

From Dev

How to find the substring \' in string, PHP

From Dev

PHP use regex to return substring

From Dev

PHP catching substring between delimiters

From Dev

Getting a substring of varying length in php

From Dev

string to substring conversion in angular js

From Dev

php find a substring only if it's not part of another substring

From Dev

How to find the string that contains substring of another string as a substring in php?

From Dev

PHP extract substring from strings in text

From Dev

PHP Validate Substring Exists with Weird Rule

From Java

How to extract random substring from a string in php?

From Dev

PHP replace preceding substrings of some other substring

From Dev

Most efficient way to extract a substring of a string in PHP

From Dev

substring in php whether words is first or last in string

From Dev

PHP double sort array based on substring

From Dev

PHP traverse a string and line break at certain substring

From Dev

PHP replace preceding substrings of some other substring

From Dev

Get the next immediate numeric substring in a php string

From Dev

specifics replace substring in string with php function

From Dev

php simple regex search and replace and shuffle substring

Related Related

  1. 1

    php cannot work with a substring

  2. 2

    PHP: capture a multiline substring

  3. 3

    Substring in php or in sql query

  4. 4

    Search substring in a PHP Array

  5. 5

    In PHP substring return �� these type of character?

  6. 6

    Replace substring in keys of an array in PHP

  7. 7

    Replace substring with an elaboration of $1 in PHP

  8. 8

    Check substring in string array in PHP

  9. 9

    php: extracting substring from a string

  10. 10

    In PHP substring return �� these type of character?

  11. 11

    How to find the substring \' in string, PHP

  12. 12

    PHP use regex to return substring

  13. 13

    PHP catching substring between delimiters

  14. 14

    Getting a substring of varying length in php

  15. 15

    string to substring conversion in angular js

  16. 16

    php find a substring only if it's not part of another substring

  17. 17

    How to find the string that contains substring of another string as a substring in php?

  18. 18

    PHP extract substring from strings in text

  19. 19

    PHP Validate Substring Exists with Weird Rule

  20. 20

    How to extract random substring from a string in php?

  21. 21

    PHP replace preceding substrings of some other substring

  22. 22

    Most efficient way to extract a substring of a string in PHP

  23. 23

    substring in php whether words is first or last in string

  24. 24

    PHP double sort array based on substring

  25. 25

    PHP traverse a string and line break at certain substring

  26. 26

    PHP replace preceding substrings of some other substring

  27. 27

    Get the next immediate numeric substring in a php string

  28. 28

    specifics replace substring in string with php function

  29. 29

    php simple regex search and replace and shuffle substring

HotTag

Archive