Jump to content

using Strrchr to get end of string


Go to solution Solved by mallen,

Recommended Posts

I'm trying to take the value and pick out everything after the trailing slash. I tested and the variable is correct. On the third line I can't figure out how to format it.

var_dump($image);// http://somewebsite.com/images/image1.jpg

var_dump(substr(strrchr('http://somewebsite.com/images/image1.jpg', '/'), 1)); //image1.jpg

var_dump(strrchr(strrchr('$image','/'),1));//getting false

Link to comment
https://forums.phpfreaks.com/topic/301079-using-strrchr-to-get-end-of-string/
Share on other sites

For what it's worth, this worked for me:

<?php
var_dump(substr(strrchr('http://somewebsite.com/images/image1.jpg', '/'), 1));
 
$image = 'http://somewebsite.com/images/image1.jpg';
var_dump($image);
var_dump(substr(strrchr($image,'/'),1));
?>

Ok I am following you. But in your example you assigned a value to $image.  But I already have a value to $image. And if I dump it is gives me the string. So I need to do the reverse.

 

I just added the $image variable to test the code.

 

What does your current code look like? Note that you'll want to make sure that strrchr() isn't being called twice and the $image variable isn't enclosed with single quotes. Could you also post the line that defines $image?

  • Solution


$THEURL = "SELECT Image_URL From ... ect";//the query

$image = $wpdb->get_row($THEURL, ARRAY_A);//the variable is dynamic

var_dump(substr(strrchr($image,'/'),1)); fails

var_dump(substr(strrchr('http://somewebsite.com/images/image1.jpg','/'),1)); gives 'image1.jpg'

var_dump($image); // gives 'http://somewebsite.com/images/image1.jpg'


I figured it out:

var_dump(substr(strrchr($image['Image_URL'],'/'),1));
Edited by mallen

Does $wpdb->get_row() return a string...or some other type of variable like an array or object? What is the exact output of

var_dump($image);

It should look something like this, if it's a string:

string(40) "http://somewebsite.com/images/image1.jpg"

Also, is PHP set to display all errors and warnings? Note that you can add the following to the top of your script during the debugging process:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
I figured it out:

var_dump(substr(strrchr($image['Image_URL'],'/'),1));

 

I'm glad you figured it out.  :happy-04:

 

Note that I marked the topic as solved. If you need anything else, you can start a new topic...or mark this one as unsolved (if it's directly related).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.