LoginSignup
21
16

More than 5 years have passed since last update.

PythonとPHPで文字列分割をして配列の最後の要素を取得

Last updated at Posted at 2014-01-06

たまにやる処理

https://qiita.com/n0bisuke/items
といったURLから最後の部分を抜き出したい

PHP

split.php
$url = "https://qiita.com/n0bisuke/items";
$tmp = explode("/", $url);
$end_word = end($tmp);

Python

split.py
url = "https://qiita.com/n0bisuke/items"
tmp = url.split('/')
end_word = tmp[-1]

Pythonの場合は[-1]を指定することで最後尾から数えることが出来るってのがPHPには無い特徴かも.[-2]を指定すれば最後尾から二番目を取得することができる

21
16
8

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
16