Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP equivalent to JavaScript's string split method

Tags:

javascript

php

I'm working with this on 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 the numeric ID and gives me 415321306.

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

Best regards.

like image 215
m4k00 Avatar asked Nov 30 '22 04:11

m4k00


2 Answers

Who needs regex?

<?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
?>
like image 36
drudge Avatar answered Dec 09 '22 16:12

drudge


Use PHP's explode() function instead of .split().

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

becomes

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

An use preg_match() instead of .match().

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

I'm a little unclear on what you're doing with the length of the string, but you can get substrings in php with substr().

like image 169
Surreal Dreams Avatar answered Dec 09 '22 17:12

Surreal Dreams