My urls for posts in WordPress looks like this:
http://localhost:8888/blabla/book/yes-vi-testar
Using the_permalink() would generate "http://localhost:8888/blabla/book/yes-vi-testar" but I want to cut the first 34 characters to get a string like "yes-vi-testar". How do I use php substr in a case like this? I'm confused... I tried
<?php
$friendlypermalink = substr(the_permalink(), 34);
?>
but that doesnt do the trick.
Use get_the_permalink to get the permalink without echoing it
So
substr(get_the_permalink(), .............);
A lot of of the Wordpress function have 'return' alternates using get as the operative word. IE: get_the_time, get_the_content, etc.
the_title is the only one I believe that doesn't have this option. For the_title you have to pass two empty parameters (the before and after seperators) and either a true or false ... not sure at the moment
the_title("","",true);
As Chacha says, use get_the_permalink(). You can then do something like:
$url = get_the_permalink();
$text = substr($url, strrpos($url, '/') + 1);
//or
preg_match('~[^/]+$~', get_the_permalink(), $m);
$text = $m[0];
Related
So, I have dynamically generated pages that follows the following format:
http://example.com/name/first_name/last_name/John%2C+Smith
What is the php code to output the last part of the url?
so, it becomes like "John, Smith".
Thank you so much.
EDIT:
I realized that the URL is ended with another / and the answers given below does not pick it up. What change should I make?
http://example.com/name/first_name/last_name/John%2C+Smith/
EDIT 2:
So, the link is dynamically generated as the following:
href="http://example.com/name/first_name/last_name/<?php echo $full_name ?>"
Split the url, get the last fragment then URL decode it:
<?
$urlarray=explode("/",$url);
$end=$urlarray[count($urlarray)-1];
$end=urldecode($end);
//go on using $end then
?>
You could do this with a regex.
echo preg_replace_callback('~.*/(.*)~',
function($matches) {
return urldecode($matches[1]);
},
'http://example.com/name/first_name/last_name/John%2C+Smith');
Regex demo: https://regex101.com/r/bE3bO5/1
Output:
John, Smith
Update:
echo preg_replace_callback('~.*/(.+)~',
function($matches) {
return rtrim(urldecode($matches[1]), '/');
},
'http://example.com/name/first_name/last_name/John%2C+Smith/');
You can use parse_url with second parameter PHP_URL_PATH
$url = urldecode("http://example.com/name/first_name/last_name/John%2C+Smith");
$arr = array_filter(explode('/',parse_url($url, PHP_URL_PATH)));
print_r(end($arr));
Edited:
As per requirement for dynamic url you can use
$url = urldecode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
I'm not sure if I'm going about this the correct way. I would like to take a simple link, like this;
https://www.youtube.com/watch?v=examplevideo
and turn it into
<a href= 'https://www.youtube.com/embed/examplevideo' target=_blank><img src='http://img.youtube.com/vi/examplevideo/0.jpg' width='536' border='1'></a>
In the past, I've been able to change links by using a str_replace, which was very straightforward since you would pull out one pattern and just replace it with another. But, in this case, the pattern that's being kept shows up twice in the output. Is a str_replace the right way to go about it?
Here's one simple way to do it...
// $video_url = "https://www.youtube.com/watch?v=examplevideo";
$videoId = str_replace("https://www.youtube.com/watch?v=", "", $video_url);
enter code here
$videoLink = "<a href= 'https://www.youtube.com/embed/$videoId' target=_blank><img src='http://img.youtube.com/vi/$videoId/0.jpg' width='536' border='1'></a>"
Of course, if your URL is more complex (e.g. ?v=abc&t=123) then this won't work, and you would have to parse the URL more like a URL (i.e. not using str_replace).
You can use parse_url() and parse_str() to get the video ID, and then use sprintf() to build the embed code.
I've made a small function:
function getEmbedded($url) {
$parts = parse_url($url);
$parsed = parse_str($parts['query'], $params);
$result = sprintf("<a href= 'https://www.youtube.com/embed/%s'
target=_blank><img src='http://img.youtube.com/vi/%s/0.jpg'
width='536' border='1'></a>", $params['v'],$params['v']);
return $result;
}
Usage:
echo getEmbedded($url);
This is more efficient than using str_replace() and works even when there are additional query parameters in the video URL.
Currently I have a url thats like this,
http://website.com/type/value
I am using
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
$array = explode('/',$url);
this to get the value currently but my page has Facebook like's on it and when it is clicked it adds all these extra variables. http://website.com/type/value?fb_action_ids=1234567&fb_action_types= and that breaks that value that I am trying to get. Is there another way to get the specific value?
Assuming you know that this will always be a valid URL, you can use parse_url.
list(, $value) = explode('/', parse_url($url)['path']);
I'd use a preg_replace
explode('/', preg_replace('/?.*$/', '', $url));
You could also use:
$array = explode('/',$_SERVER['PATH_INFO']);
Or, this:
$array = explode('/',$_SERVER['PHP_SELF']);
With this, you do not need the trim() call or the temp var $url - unless you use it from something else.
The reason for two options is I don't know if /type/value is being passed to an index.php or if value is in fact a php file. Either way, one of the two options will give you what you need.
I'm trying to limit the string output of get_the_content but I can't find anywhere on the net on how to do this.
Everything I find is regarding the_content().
I'm not using the_content because I want the string to be unformatted and because for some reason it doesn't seem to work right on my loop for all the posts I have.
Anyways, does anyone no how to make get_the_content return only a specified number of characters of the actual description? I don't want to resort to using the excerpt as that is reserved for some other information I'm using.
I haven't tested this but I think it will work..
Go to wp-includes/post-template.php
Find the get_the_content() function
At the end of the function, there is
return $output;
Before that final line, add
$output = preg_replace("/((\S+\s+){1,13}).*/s","\\1",strip_tags($output));
So you're left with
$output = preg_replace("/((\S+\s+){1,13}).*/s","\\1",strip_tags($output));
return $output;
The part you'll want to change is the number "13" in the code above - just put the number of words you want to display
Let me know how that works for you
try this. It worked of me
<?php $mycontent = get_the_content();
$trimmed_content = wp_trim_words( $mycontent , 50, '...[ read more ]' ); ?>
<p><?php echo $trimmed_content; ?></p>
(Change the 50 to your desire words length)
try substr(),
$description = substr(get_the_content(), 0, $number_of_characters);
I've run into a hard problem to deal with. I am replacing a-tags and img-tags to fit my suggestions like this. So far so good.
$search = array('|(<a\s*[^>]*href=[\'"]?)|', '|(<img\s*[^>]*src=[\'"]?)|');
$replace = array('\1proxy2.php?url=', '\1'.$url.'/');
$new_content = preg_replace($search, $replace, $content);
Now my problem is that there are links on pages that i fetch the content of that looks like this:
<a href="/test/page/">
and
<a href="http://google.se/test/">
And when after replacing these two links looks like this:
<a href="proxy2.php?url=/test/page/">
and
<a href="proxy2.php?url=http://google.se/test/">
The problem is for me is that i want to include a variable named $url before /test/page/ and only on that links that are like that, not those who was already http:// or https:// before.
This should do the job for the anchor tags, at least:
<?php
function prepend_proxy($matches) {
$url = 'http://example.prefix';
$prepend = $matches[2] ? $matches[2] : $url;
$prepend = 'proxy2.php?url='. $prepend;
return $matches[1] . $prepend . $matches[3];
}
$new_content = preg_replace_callback(
'|(href=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
'prepend_proxy',
$content
);
?>
Simply make your proxy2.php a little smarter. If a fully qualified URL comes in (http://...), redirect to that. If a local URL comes in (e.g. /test/page/), drop in what's missing (e.g. http://www.mylittleapp.com/test/page/) and redirect.
This would do the trick
$search = array('#(<a\s*[^>]*href=[\'"]?)(https?://)?#');
$replace = array('\1proxy2.php?url=');
$new_content = preg_replace($search, $replace, $content);
Result:
<a href="proxy2.php?url=/test/page/">
<a href="proxy2.php?url=google.se/test/">
it's me Sara. Scronide, your code did'nt work. It still returns:
<a href="proxy2.php?url=/test/page/">
<a href="proxy2.php?url=google.se/test/">
Instead of what i wanted it to show, i wanted it to show like this, with the url prepended:
<a href="proxy2.php?url=**THEURLHERE.COM**/test/page/">
<a href="proxy2.php?url=google.se/test/">
SORRY, IT DID WORK, I WAS DOING SOMETHING WRONG WITH THE URL VARIABEL. THANK U SCRONIDE!