i'm using php wordwrap for my comment box.
This is my clickable function,
function clickable_link($text)
{
$ret = ' ' . $text;
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?#\[\]+]*)#is", "\\1<a class=\"hrefLink\" href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?#\[\]+]*)#is", "\\1<a class=\"hrefLink\" href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)#([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1\\2#\\3", $ret);
$ret = substr($ret, 1);
return $ret;
}
and this is my wordwrap for comment
$comment = clickable_link($comment);
$comment = wordwrap($comment, 25, "\n", false);
so, once the word limit for 25 is reached, my comment box break my link
http://www.websitetitle.com/showthread.php?t=2000
link become like this
http://www.websitetitle.com/showthread.php?
<br>
t=2000
The link is broken. so is it possible to fix the link or any other workaround?
Thank you
The wordwrap function is breaking the link.
If you're trying to restrict the width of the comment box then I would suggest you do it within CSS rather than rely on the wordwrap function.
Related
I'm using a Smarty modifier to turn a plaintext link into a proper hyperlink, I'm using a Smarty modifier for this as it's for a website that utilizes user content, in which only some areas are allowed to have hyperlinks.
This is the modifier:
function smarty_modifier_dolink($text)
{
$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
$ret = ' ' . $text;
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?#\[\]+]*)#is", "\\1\\2", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?#\[\]+]*)#is", "\\1\\2", $ret);
//$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)#([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1\\2#\\3", $ret);
$ret = substr($ret, 1);
return $ret;
}
?>
The code for the modifier was shared on another website. It works fine but doesn't work when the plaintext link is in parentheses, any help would be appreciated, thanks!
One extracted PCRE regex:
(^|[\n ])\(?((www|ftp)\.[\w\#$%&~\/.\-;:=,?#\[\]+]*)\)?
regex tested on regex101.com with url
(www.example.com/test/test.html)
collection group2 -> www.example.com/test/test.html
So the following should work:
$ret = preg_replace("#(^|[\n ])\(?((www|ftp)\.[\w\#$%&~/.\-;:=,?#\[\]+]*)\)?#is", "\\1\\2", $ret);
$ret = preg_replace("#(^|[\n ])(?([\w]+?://[\w#$%&~/.-;:=,?#[]+]*)(?#is", "\1<a href="\2" target="_blank" rel="nofollow">\2", $ret);
Explanation: I have added
\)?
/) for a bracket and ? for optional at the positions where i suggested your brackets to be.
I am working with PHP and WordPress right now, I need to basically run the below code to Replace text in $current_path with the text in $new_path if $current_path EXIST in $content
I would prefer to be able to iterate over an array instead of running this over and over like this, or any better method would be nice?
$content = 'www.domain.com/news-tag/newstaghere'
$current_path = 'test-tag';
$new_path = 'test/tag';
$content = str_replace($current_path, $new_path, $content);
$current_path = 'news-tag';
$new_path = 'news/tag';
$content = str_replace($current_path, $new_path, $content);
$current_path = 'ppc-tag';
$new_path = 'ppc/tag';
$content = str_replace($current_path, $new_path, $content);
str_replace() accepts array arguments:
$current_paths = array('test-tag','news-tag','ppc-tag');
$new_paths = array('test/tag','news/tag','ppc/tag');
$new_content = str_replace($current_paths, $new_paths, $content);
Or you can use a single array with strtr():
$path_map = array('test-tag'=>'test/tag', 'news-tag'=>'news/tag', 'ppc-tag'=>'ppc/tag');
$new_content = strtr($content, $path_map);
However, you seem to be doing something very generic. Maybe all you need is a regex?
$new_content = preg_replace('/(test|news|ppc)-(tag)/u', '\1/\2', $content);
Or maybe even just
$new_content = preg_replace('/(\w+)-(tag)/u', '\1/\2', $content);
$content = 'www.domain.com/news-tag/newstaghere'
$current_paths = array('test-tag','news-tag','ppc-tag');
$new_paths = array('test/tag','news/tag','ppc/tag';
$content = str_replace($current_paths, $new_paths, $content);
Array arguments can be provided for the str_replace function, as noted on the following PHP.net page:
http://php.net/manual/en/function.str-replace.php
Please see "Example #2" on the page linked above for details.
You can do that:
$content = 'www.domain.com/news-tag/newstaghere';
$content = preg_replace('~www\.domain\.com/\w++\K-(?=tag/)~', '/', $content);
Overnight my website has decided to produce the following error:
Fatal error: Call to undefined method WP_Error::get_items() in
/home/xxx/public_html/wp-content/themes/xxx/functions.php on line 494
The error breaks the twitter feed which stops the rest of the page rendering. I didn't implement the code for this site, but the code thats upsetting things appears to be:
function twitterify($ret) {
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1\\2", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1\\2", $ret);
$ret = preg_replace("/#(\w+)/", "#\\1", $ret);
$ret = preg_replace("/#(\w+)/", "#\\1", $ret);
return $ret;
}
Which has been pulled from this website.
Any ideas as to how this could be resolved? I hear that Twitter has changed their API, could this be the issue?
This seems to have something to do with an RSS fetch_feed() function. When fetch_feed() is successful, it returns a SimplePie class that has a method of get_items(). However, if fetch_feed fails, it returns a WP_Error class, that DOES NOT have a method of get_items(), and now you're calling an undefined function.
This might look like:
$rss = fetch_feed($url);
$rss->get_items();
In this example, $rss will be a WP_Error class if the fetch_feed() function fails...
I'm outputting a Twitter feed using the variable $tweet, when it outputs the tweet it stops when it hits an # or hastag, is it possible to just output those as normal text?
E.g
This is what happens when it tries to show a #
(i.e as soon as it hits the # it stops, the same applies to a hashtag)
UPDATE:
<?php
function parseTweet($text) {
$pattern_url = '~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?#)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i';
'#([A-Za-z0-9_]+)';
$tweet = preg_replace('/(^|\s)#(\w+)/', '\1#\2', $text);
$tweet = preg_replace('/(^|\s)#(\w+)/', '\1#\2', $tweet);
$tweet = preg_replace("#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?#\[\]+]*)(/[\w\#$%&~/.\-;:=,?#\[\]+]*)?)#is", "\\1[link]", $tweet);
return $tweet;
}
$username='teamworksdesign'; // set user name
$format='json'; // set format
$tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); // get tweets and decode them into a variable
$theTweet = parseTweet($tweet[0]->text);
$newTweet = substr($theTweet,0,65);
echo '<a class="tweet" rel="nofollow" href="http://www.twitter.com/teamworksdesign"> "' . $newTweet . '..."</a>';
?>
EDIT: The answer is in the comments.
Some parts of that function appear to be unnecessary. You could have a go with this function instead, as I know this one works?
function parseTweet($ret) {
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1\\2", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1\\2", $ret);
$ret = preg_replace("/#(\w+)/", "#\\1", $ret); // Usernames
$ret = preg_replace("/#(\w+)/", "#\\1", $ret); // Hash Tags
return $ret;
}
Now - not knowing exactly where your problem is, this is what i do when i am reading a tweet to display as HTML:
// filter the user's username out of tweets
$tweet = str_replace($username . ": ", "", $tweet);
// turn URLs into hyperlinks
$tweet = preg_replace("/(http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*)/", "Link", $tweet);
// link to users in replies
$tweet = preg_replace("(#([a-zA-Z0-9\_]+))", "\\0", $tweet);
// add the time posted
$tweet = $tweet . " <span class=\"tweetwhen\">" . $posted . "</span>";
I've got a nice little twitter widget on my website that was created using php. I'd like to be able to make sure that when a link appears it is clickable or when I #reply someone it links to their profile. Any help is greatly appreciated.
<?php
function getTwitterStatus($userid){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=1";
$xml = simplexml_load_file($url) or die("could not connect");
foreach($xml->status as $status){
$text = $status->text;
}
echo $text;
}
getTwitterStatus("UltanKC");
?>
I have a function which I use for this - it will make links for URLs, twitter user names and twitter hashtags.
function auto_link_twitter ($text)
{
// properly formatted URLs
$urls = "/(((http[s]?:\/\/)|(www\.))?(([a-z][-a-z0-9]+\.)?[a-z][-a-z0-9]+\.[a-z]+(\.[a-z]{2,2})?)\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1})/is";
$text = preg_replace($urls, " <a href='$1'>$1</a>", $text);
// URLs without protocols
$text = preg_replace("/href=\"www/", "href=\"http://www", $text);
// Twitter usernames
$twitter = "/#([A-Za-z0-9_]+)/is";
$text = preg_replace ($twitter, " <a href='https://twitter.com/$1'>#$1</a>", $text);
// Twitter hashtags
$hashtag = "/#([A-Aa-z0-9_-]+)/is";
$text = preg_replace ($hashtag, " <a href='https://twitter.com/hashtag/$1'>#$1</a>", $text);
return $text;
}
to use it with your code, edit the line which echoes out the status:
echo auto_link_twitter ($text);
Looks like this one is pretty comprehensive via googling 'twitter php automatic links'
(from http://www.snipe.net/2009/09/php-twitter-clickable-links/):
function twitterify($ret) {
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1\\2", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1\\2", $ret);
$ret = preg_replace("/#(\w+)/", "#\\1", $ret);
$ret = preg_replace("/#(\w+)/", "#\\1", $ret);
return $ret;
}