Output # and # as normal text on Twitter feed - php

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>";

Related

Smarty modifier to add hyperlinks doesn't work when text is in parentheses

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.

Extracting words from a text using php

Hello friends have a little problem. I need to extract only the words of a text "anyone".
I tried to retrieve the words using strtok (), strstr (). some regular expressions, but only managed to extract some words.
The problem is complex due to the number of characters and symbols that can accompany the words.
The example text which must be extracted words. This is a sample text:
Main article: our 46,000 required, !but (1947-2011) mail#server.com March 8, 2014 Gutenberg's 34-DE 'a' 3,1415 Us: #unknown n go http://google.com or www.google.com and http://www.google.com (r) The 509th "composite" and; C-54 #dog v4.0 ¿as is done? ¿article... agriculture? x ¿cat? now! Hi!! (87 meters).
Sample text, for testing.
The result of extracting the text should be:
Main article our required but March Gutenberg's a go or and The composite and dog as is done article agriculture cat now Hi meters
Sample text for testing
The first function I wrote to facilitate the work
function PreText($text){
$text = str_replace("\n", ".", $text);
$text = str_replace("\r", ".", $text);
$text = str_replace("'", "", $text);
$text = str_replace("?", "", $text);
$text = str_replace("¿", "", $text);
$text = str_replace("(", "", $text);
$text = str_replace(")", "", $text);
$text = str_replace('"', "", $text);
$text = str_replace(';', "", $text);
$text = str_replace('!', "", $text);
$text = str_replace('<', "", $text);
$text = str_replace('>', "", $text);
$text = str_replace('#', "", $text);
$text = str_replace(",", "", $text);
$text = str_replace(".c", "", $text);
$text = str_replace(".C", "", $text);
return $text;
}
Split function:
function SplitWords($text){
$words = explode(" ", $text);
$ContWords = count($words);
for ($i = 0; $i < $ContWords; $i++){
if (ctype_alpha($words[$i])) {
$NewText .= $words[$i].", ";
}
}
return $NewText;
}
The program:
<?
include_once ('functions.php');
$text = "Main article: our 46,000 ...";
$text = PreText($text);
$text = SplitWords($text);
echo $text;
?>
Is that the code has a long way. We appreciate your help.
If I understand you correctly, you want to remove all non-letters from the string. I would use preg_replace
$text = "Main article: our 46,000...";
$text = preg_replace("/[^a-zA-Z' ]/","",$text);
This should remove everything that is not a letter, apostrophe or a space.
Try this almost your requirement
<?php
$text = <<<HEREDOC
Main article: our 46,000 required, !but (1947-2011) mail#server.com March 8, 2014 Gutenberg's 34-DE 'a' 3,1415 Us: #unknown n go http://google.com or www.google.com and
http://www.google.com (r) The 509th composite" and; C-54 #dog v4.0 ¿as is done? ¿article... agriculture? x ¿cat? now! Hi!! (87 meters). Sample text, for testing.
HEREDOC;
//replace all kind of URLs and emails from text
$url_email = "((https?|ftp)\:\/\/)?"; // SCHEME
$url_email .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?#)?"; // User and Pass
$url_email .= "([a-z0-9-.]*)\.([a-z]{2,4})"; // Host or IP
$url_email .= "(\:[0-9]{2,5})?"; // Port
$url_email .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
$url_email .= "(\?[a-z+&\$_.-][a-z0-9;:#&%=+\/\$_.-]*)?"; // GET Query
$url_email .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
$text = preg_replace("/$url_email/","",$text);
//replace anything like Us: #unknown
$text = preg_replace("/Us:.?#\\w+/","",$text);
//replace all Non-Alpha characters
$text = preg_replace("/[^a-zA-Z' ]/","",$text);
echo $text;
?>

Need to validate "last tweet" script

Have been searching for a solution for hours.
My entire WordPress theme validates, except this script I'm using to receive the last tweet:
<?php
$twitterUsername = get_option('of_twitter_username');
$username = $twitterUsername; // Your twitter username.
$prefix = ""; // Prefix - some text you want displayed before your latest tweet.
$suffix = ""; // Suffix - some text you want display after your latest tweet.
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace("<", "<", $tweet);
$tweet = str_replace(">", ">", $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>
The error, it seems, is:
$tweet = str_replace(">", ">", $tweet);
Not sure how to fix this.
Thanks for any help.
Replace the two str_replace calls with:
$tweet = html_entity_decode($tweet);
Maybe a simplier way (you don't need to parse) is to load http://search.twitter.com/search.json?q=from:the_username and make a json_decode of the result.
Then you can get the last tweet easily.

Add links to PHP that pulls latest tweet from Twitter Account

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;
}

link clickable and wordwrap

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.

Categories