var sURL = "http://itunes.ap...">

PHP equivalent of JavaScript string split method

I work with this in JavaScript:

<script type="text/javascript">
    var sURL = "http://itunes.apple.com/us/app/accenture-application-for/id415321306?uo=2&mt=8&uo=2";
    splitURL = sURL.split('/');
    var appID = splitURL[splitURL.length - 1].match(/[0-9]*[0-9]/)[0];
    document.write('<br /><strong>Link Lookup:</strong> <a href="http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=' + appID + '&country=es" >Lookup</a><br />');
</script>

This script takes a numeric identifier and gives me 415321306.

So my question is how can I do the same, but using PHP.

Sincerely.

+3
source share
5 answers

Use PHP explode () instead of .split ().

splitURL = sURL.split('/');  //JavaScript

becomes

$splitURL = explode('/', $sURL);  //PHP

Use preg_match () instead of .match ().

$appID = preg_match("[0-9]*[0-9]", $splitURL);

I don't understand a bit what you are doing with string length, but you can get substrings in php with substr () .

+10
source

Who needs a regular expression?

<?php
    $sURL = "http://itunes.apple.com/us/app/accenture-application-for/id415321306?uo=2&mt=8&uo=2";
    $appID = str_replace('id','',basename(parse_url($sURL, PHP_URL_PATH)));
    echo $appID; // output: 415321306
?>
+5
source
preg_match("/([0-9]+)/",$url,$matches);
print_r($matches);
+2

Javascript split ( ), RegExp.

/*

Example 1

This can be done with php function str_split();

*/

var str = "Hello World!"

str.split('');

H,e,l,l,o, ,W,o,r,l,d,!

/*

Example 1

This can be done with php function preg_split();

*/

var str = " \u00a0\n\r\t\f\u000b\u200b";

str.split('');

, , , , ,,,​

Ecma-262 Array, String. ; - , . RegExp (.. , [[Class]] "RegExp"; . 15.10). String, . String, . (, , ; , .) , String , . (, "ab".split(/a *?/) [ "a", "b" ], "ab".split(/a */) ["", "b" ].) ( ) , , . , . , . , , ( undefined) .

+1
source

Source: https://habr.com/ru/post/1793167/


All Articles