Jump to content

using Strrchr to get end of string


mallen
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
Share on other sites

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?

Link to comment
Share on other sites

  • 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
Link to comment
Share on other sites

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);
?>
Link to comment
Share on other sites

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.