I'm trying to strip a variable suffix off my Wordpress titles, basically a bunch of hastags from posts imported from Instagram that are preceeded by an hyphen and I've come up with something like this:
<?php
$titleoutput = get_the_title($ID);
$titleoutputstrip = preg_replace("/\-/", "", $titleoutput);
echo $titleoutputstrip;
?>
But it doesn't seem to be working.
Can anyone help me out?
Thanks in advance!
<?php
$titleoutput = get_the_title($ID);
$titleoutputstrip = ltrim($titleoutput , '-'); // ltrim for begining rtrim for end
echo $titleoutputstrip;
?>
I finally nailed it with:
<?php
$titleoutput = get_the_title($ID);
$titleoutputstrip = preg_replace('/\ #[^.]+$/','',$titleoutput);
echo $titleoutputstrip;
?>
The hyphen was giving me problems because it's actually a dash "–" or something like that so I just set it to look for the first hashtag and erase the contents to the end.
Related
I'm trying to use a regex to find and replace all URLs in a forum system. This works but it also selects anything that is within bbcode. This shouldn't be happening.
My code is as follows:
<?php
function make_links_clickable($text){
return preg_replace('!(([^=](f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i', '$1', $text);
}
//$text = "https://www.mcgamerzone.com<br>http://www.mcgamerzone.com/help/support<br>Just text<br>http://www.google.com/<br><b>More text</b>";
$text = "#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa";
echo "<b>Unparsed text:</b><br>";
echo $text;
echo "<br><br>";
echo "<b>Parsed text:</b><br>";
echo make_links_clickable($text);
?>
All urls that occur in bb-code are following up on a = character, meaning that I don't want anything that starts with = to be selected.
I basically have that working but this results in selecting 1 extra character in in front of the string that should be selected.
I'm not very familiar with regex. The final output of my code is this:
<b>Unparsed text:</b><br>
#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa<br>
<br>
<b>Parsed text:</b><br>
#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa
You can match and skip [url=...] like this:
\[url=[^\]]*](*SKIP)(?!)|(((f|ht)tps?://)[-a-zA-Zа-яёЁА-Я()0-9#:%_+.\~#?&;/=]+)
See regex demo
That way, you will only match the URLs outside the [url=...] tag.
IDEONE demo:
function make_links_clickable($text){
return preg_replace('~\[url=[^\]]*](*SKIP)(?!)|(((f|ht)tps?://)[-a-zA-Zа-яёЁА-Я()0-9#:%_+.\~#?&;/=]+)~iu', '$1', $text);
}
$text = "#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa";
echo "<b>Parsed text:</b><br>";
echo make_links_clickable($text);
You can use a negative lookbehind (?<!=) instead of your negated class. It asserts that what is going to be matched isn't preceded by something.
Example
Is there a way to remove the blank space between the words in the the_titel?
Example:
the_title() on one of my posts results in Merry Christmast
I want it to result in merrychristmast
Which means that I want to remove the blank space and use lowercase only.
Thanks
Edit: I was actually looking for a solution to the the_title-tag not the wp_title-tag. Sorry..
Doing it for wp_title();
I combined those two answers posted by Anthony and rzetterberg.
Use str_replace();. It's faster for trivial replacements than RegEx. And if you add the necessary arguments to your wp_title(); we'll end up like this. Please note, that you'll have to add the strtolower(); function so that your title is displayed in lower case only.
<?php
echo strtolower(str_replace(' ', '', wp_title('', false)));
?>
Doing it for the_title();
It's quite the same technique I posted earlier. You'll just have to change the arguments for the_title($before, $after, $echo);.
<?php
echo strtolower(str_replace(' ', '', the_title('', '', false)));
?>
Note: Instead of using the_title('', '', false) you could prepend it with a get_. It does the same and fits your needs better.
<?php
echo strtolower(str_replace(' ', '', get_the_title()));
?>
get title -> remove white spaces (preg_replace) -> to lower case (strtolower)
<?php echo strtolower(preg_replace('/\s+/', '', wp_title("",false))); ?>
No, you would have to retrieve the value and remove the spaces yourself.
Like so:
$title = wp_title("", false);
Read more about the arguments for wp_title here
link is : info.php?Submit=#img.png
so
<?php echo $_GET["Submit"]; ?>
but this wil show : #img.png
how to remove the "#" from the name so it shows : img.png ??
thanks
$_GET["Submit"] will not contain that, since # marks the beginning of the 'fragment', which does not get passed to the server.
If the link is info.php?Submit=%23img.png, then you can trim it like this:
<?php echo substr($_GET["Submit"], 1); ?>
Well, you could always modify the sender code to exclude the leading # (or %23 as #Cal pointed out).
Otherwise, try one of these:
//substring [1:len]
$yourString = substr($_GET["Submit"], 1);
//replace "#" with ""
$yourString = str_replace("%23", "", $_GET["Submit"], 1); //1 is the limit of #s to remove
//parse the URL, then get the path
$yourString = parse_url($_GET["Submit"], PHP_URL_PATH);
Try this, if you are sure that the first character is either a # or the encoding of one (%23):
<?php echo substr(urldecode($_GET["Submit"]),1); ?>
I want to get all the image address which is a letter.
I use this...
<?php
$str = <<<EOT
image/20110331_121.jpg
../image/20110330_132.jpg
http://www.site.com/image/20110330_098.jpg
EOT;
$image = preg_match('#^[a-zA-Z](.*)\/.(jpg)$#i',$str);
print_r($image);// I want get a echo image/20110331_121.jpg
?>
Your problem was the \/ right before the .jpg without any placeholder. You could try this:
$image = preg_match('#^[a-z]\w+/\w+[.](jpg)$#im',$str);
And you also forgot the #m modifier to apply ^ and $ against multiple lines.
I have a variable $link_item, it's used with echo and gives the strings like
<span class="name">Google</span>http://google.com
How to remove "<span class="name">Google</span>" from string?
It should give just "http://google.com".
Heard it can be done with regex(), please help.
Without regex:
echo substr($link_item, stripos($link_item, 'http:'))
But this only works if the first part (i.e. <span class="name">Google</span>) never contains http:. If you can assure this: here you go :)
Reference: substr, stripos
Update:
As #Gordon points out in his comment, my code is doing the same as strstr() already does. I just put it here in case one does not read the comments:
echo strstr($link_item, 'http://');
$string = '<span class="name">Google</span>http://google.com';
$pieces = explode("</span>",$string);
//In case there is more than one span before the URL
echo $pieces[count($pieces) -1];
Solved:
$contents = '<span class="name">Google</span>http://google.com';
$new_text = preg_replace('/<span[^>]*>([\s\S]*?)<\/span[^>]*>/', '', $contents);
echo $new_text;
// outputs -> http://google.com
Don't use a regex. Use a HTML parser to extract only the text you want from it.
Made myself
$link_item_url = preg_replace('#<span[^>]*?>.*?</span>#si', '', $link_item);
This will remove any <span + something + </span> from variable $link_item.
Thanks for all.