Using the following in PHP:
var_dump($obj->denormalized->{'https://api.site.com/user/user-76643221'}->data->avatar_image)
Results:
string(56) "https://api.site.com/image_link/user-76643221_b691647a"
I only need the link, without those extra parts. Any ideas?
var_dump shows the types and sizes of variables; it's meant for debugging. If you only need the link, then instead of using var_dump use print or echo
echo $obj->denormalized->{'https://api.site.com/user/user-76643221'}->data->avatar_image;
Results:
https://api.site.com/image_link/user-76643221_b691647a
Related
How to add " string into the first and last in PHP,
something like this :
hello world
into like this :
"hello" "world"
is there before-string and after-string function in PHP?
In PHP there are two basic ways to get output: echo and print.
In this tutorial we use echo (and print) in almost every example. So, this chapter contains a little more info about those two output statements.
For more datailes use http://www.w3schools.com/php/php_echo_print.asp
Simply use it like as
echo '"Hello" "World"';
You can simply use preg_replace like as
echo preg_replace('/(\w+)/',"\"$1\"","hello world");
Demo
try this:
<?php
$str="hello world";//your string
$str=str_replace(" ",'" "',$str); //replace all spaces with " "
echo '"'.$str.'"';//while displaying add double quotes in begning and end.
?>
There is no such function.
However you can try using split function on space of the string, then use implode() function with your glue or you can simply try using regex.Latter would give you a 1 line call to do this.
I have a PHP associative array that I am trying to extract data from:
array(18) { ["body"]=> string(34) "Hey! Let me know if you got this"}
The above array is stored in a variable called $firstChildData, and when I try to run the following line, I get the result below it:
$firstChildBody = $firstChildData["body"];
This returns: string(34) "Hey! Let me know if you got this"
Does anyone know how to remove the 'string(34)' so I can just have the value within the quotes? I have tried to use the explode() function with " as the delimiter, but that didn't work.
Thanks in advance!
string(34) is just a debug information if you use a function like var_dump or print_r.
You can print strings normal with echo or print.
echo $firstChildData["body"];
If you want to use the variable $firstChildData["body"] in any program code, it is handled as the string in it. The information string(34) ist not included.
Thanks everyone!
You're right, I was using var_dump, which was showing that information even thought it was not part of the string. I used echo instead and it works beautifully.
I'm trying to mix <?php echo do_shortcode('[...]') with a field from Advanced Custom Fields within Wordpress.
So basically what I'm trying to do is give the user a text field in the page edit screen where she can paste in the ID of a youtube vide. This field will then update my do_shortcode to display the correct video.
I'm not sure what I'm doing wrong considering I've done this several times before and been succesful. I do have a feeling I'm not escaping the string correctly?
<?php echo do_shortcode('[video_lightbox_youtube video_id="' . the_field("youtube_video") . '" width="640" height="480" anchor="Play Video"]'); ?>
Anyone able to lead me in the right direction? :)
EDIT
The code above returns q_cHD1lcEOo with multiple spaces in front of it as well as this: "Error! You must specify a value for the Video ID, Width, Height parameters to use this shortcode!" That's why I was thinking I'm not escaping it correctly as these are all specified.
I'll add that if I remove the_field("...") and replace it with just an ID it displays perfectly.
SECOND EDIT
Since I was not supposed to echo it, I was using the wrong function to get the field. Instead of using the_field() which prints the value, I was supposed to use get_field() to simply return it to the string.
Your question is somewhat unclear, but I'm also 20 hours without sleep.
Anyways, as far as mixing PHP within a PHP string, there's numerous ways to do it..
You can use concatenation or { } within the string itself.
For example, say we want to echo out the property of an object within a string.
We could do the following
echo "This is my property " . $object->property;
Or, we can do this
echo "This is my property {$object->property}";
You can even do cool things like access associative arrays within strings like so
echo "This is my property {$object->property['cool']}";
Hopefully this leads you in the ride direction.
At first glance it looks like you should be using get_field instead of the_field. the_field will print without being prompted, whereas get_field will return its value, which is what you want.
I see you've also mentioned whitespace at the start, you should consider wrapping the function in trim.
See below:
<?php echo do_shortcode('[video_lightbox_youtube video_id="' . trim(get_field("youtube_video")) . '" width="640" height="480" anchor="Play Video"]'); ?>
URL: example.com/search:#searchWord
Since the # is not send to the server, it is changed to %23.
In the search page I want to display
Results for: #searchWord
But what I get is %23searchWord. I tried htmlspecialchars(), but nothing happens.
To achieve your goal use urldecode.
echo urlencode('example.com/search:#searchWord');
// output: example.com%2Fsearch%3A%23searchWord
echo urldecode('example.com%2Fsearch%3A%23searchWord');
// output: example.com/search:#searchWord
I'm writing some code to look at PTO assignment data, and noticed a strange problem where the URL didn't work when I generated it from my database entry, but did work when I entered the same string manually. When I echo them both strings look the same exactly, but when I equate them they are different. Is there some kind of hidden character or something with the encoding I can't see, and if so how do I fix it? I'm running the code using WAMPSERVER.
$pto = "assignments.uspto.gov/assignments/q?db=pat&reel=";
$row = mysql_fetch_assoc($result);
$ReelFrame = $row['ReelFrame']; // Should be "007358/0006" and looks to be correct.
$reelframearray = explode("/",$ReelFrame);
$gourla = $pto . $reelframearray[0] . "&frame=". $reelframearray[1];
$gourl = "assignments.uspto.gov/assignments/q?db=pat&reel=007358&frame=0006";
echo "$gourla <br>";
echo "$gourl <br>";
if ($gourl === $gourla){
echo "same string";
} else {
echo "different";
}
This is what the results look like:
assignments.uspto.gov/assignments/q?db=pat&reel=007358&frame=0006
assignments.uspto.gov/assignments/q?db=pat&reel=007358&frame=0006
different
I've narrowed it down to the mysql entry since replacing that with this:
$ReelFrame = "007358/0006";
Makes the strings the same.
Thanks for any help. The column for ReelFrame is a VARCHAR if that makes any difference.
please try to var_dump() your both variables instead of echoing them - maybe there are some leading/trailing whitespaces, linebreaks or something like that.
EDIT: just as a note: you realy shouldn't save multiple values in one database-coumn. if ReelFrame consits of two values, make two colums out of it: Reel and Frame - and don't use charchar for this, it looks like they should be ints (and that will avoid you from saving whitespaces linebreaks or something like that accidentally)
The first thought that comes to my mind is \n character.
Try this:
$ReelFrame = trim($row['ReelFrame']);
I tried the trim() suggestion, but it doesn't seem to work. I'm not sure what kind of character it is inserting, but I guess trim isn't removing it.
I just tried the var_dump, and it does show the strings are different lengths though nothing else looks different.
vardump of a ReelFrame entity compared with manual string:
string(20) "007358/0006"
string(11) "007358/0006"
likewise for the final string:
string(81) "assignments.uspto.gov/assignments/q?db=pat&reel=007358&frame=0006"
string(72) "assignments.uspto.gov/assignments/q?db=pat&reel=007358&frame=0006"
trim() doesn't seem to make any difference on the entity so it's still showing 20.
Are there any other invisible characters that trim might miss?