LoginSignup
16

More than 3 years have passed since last update.

posted at

updated at

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

たまにやる処理

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]を指定すれば最後尾から二番目を取得することができる

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
What you can do with signing up
16