Spratters53
Spratters53

Reputation: 415

How to find string between two "/" characters - php

What I'm trying to do is edit the value of a folder when the title is changed. The title is directly associated with the folder and as such, for consistency, the folder needs to be updates.

What I have pre-stored so far is:

Pictures/Title/image.png

What i want to do is get out "Title" which will always be between two / characters. Then take that out of the string and replace it with the new title.

preg_match is the obvious function, but i'm unsure of how to use it to find and return the string I want.

preg_match("$title" , $ImageLocation, $matches);
$title = $matches

Ideally, that would work if i knew what the title would be but I can't figure out how to both account for the unknown string, and for the special characters.

All i know for certain is that "title", whatever that may be, will be between two "/" characters. The php docs show the use of special characters but i personally cannot fathom how to use the commands as it doesn't appear to be specific. Any help's appreciated.

Upvotes: 0

Views: 916

Answers (1)

Hanky Panky
Hanky Panky

Reputation: 46900

As long as the folder structure remains the same, you can avoid Regexes and do

<?php
$title="Pictures/Anything/image.png";
$parts=explode("/",$title);
echo $parts[1];                          //Anything

Fiddle

Upvotes: 4

Related Questions