replace arabic charset with hashtag preg_replace? - preg-replace

I've made my own PHP script and my problem with hashtags
and I've added preg_replace function to replace tags to hashtags its working well when i use English charsets but in Arabic or any other language its not working
where is the problem in my code ?
$post = #preg_replace('/(^|\s)#(\w+)/', '<a class=\"hashtag\" href="hashtag.php?tag=\2">\1#\2</a>', $post);
i want preg_repalce to replace none english tags too how ?

i found it
this is how to match arabic hastags
$post = #preg_replace('/(#\w+)/u', '<a class="hashtag" href="hashtag.php?tag=\1">\1</a>', $post);

Related

PHP Remove specific line return

After i've done a preg_replace to remove the TAG 'TagToremove', i still have a line return, would you know if either i could remove it during the preg_replace
Or After and how ?
Thanks
<?php
$strip_list = array('TagToremove');
foreach ($strip_list as $tag)
{
$temp = preg_replace('/<\/?' . $tag . '(.|\s)*?>/', '', $temp);
}
?>
String before Preg_replace:
<CodeServiceTransport></CodeServiceTransport>
<PrixTotalCommande>100</PrixTotalCommande>
<TagToremove>
<Ligne>
<ll>hh</ll>
<Id>48</Id>
<SKU>autreID</SKU>
<Quantity>1</Quantity>
</Ligne>
</TagToremove>
<Meta-CodeActivite></Meta-CodeActivite>
<Meta-CodeEnseigne></Meta-CodeEnseigne>
String after Preg_replace:
<CodeServiceTransport></CodeServiceTransport>
<PrixTotalCommande>100</PrixTotalCommande>
<Ligne>
<ll>hh</ll>
<Id>48</Id>
<SKU>autreID</SKU>
<Quantity>1</Quantity>
</Ligne>
<Meta-CodeActivite></Meta-CodeActivite>
<Meta-CodeEnseigne></Meta-CodeEnseigne>
Don't parse HTML with regex, use a DOM parser instead.
Nevertheless, for your specific problem, you have to add an optional linebreak in your regex like that:
$temp = preg_replace('~</?' . $tag . '[^>]*>\R?~i', '', $temp);
\R stands for any kind of linebreak (ie. \r or \n or \r\n)

Perl split string at character entity reference

Quick Perl question with hopefully a simple answer. I'm trying to perform a split on a string containing non breaking spaces ( ). This is after reading in an html page using HTML::TreeBuilder::XPath and retrieving the string needed by $titleString = $tree->findvalue('/html/head/title')
use HTML::TreeBuilder::XPath;
$tree = HTML::TreeBuilder::XPath->new;
$tree->parse_file( "filename" );
$titleString = $tree->findvalue('/html/head/title');
print "$titleString\n";
Pasted below is the original string and below that the string that gets printed:
Mr Dan Perkins (Active)
Mr?Dan Perkins?(Active)
I've tried splitting $titleString with #parts = split('\?',$titleString); and also with the original nbsp, though neither have worked. My hunch is that there's a simple piece of encoding code to be added somewhere?
HTML code:
<html>
<head>
<title>Dan Perkins (Active)</title>
</head>
</html>
You shouldn't have to know how the text in the document is encoded. As such, findvalue returns an actual non-breaking space (U+00A0) when the document contains . As such, you'd use
split(/\xA0/, $title_string)
-or-
split(/\x{00A0}/, $title_string)
-or-
split(/\N{U+00A0}/, $title_string)
-or-
split(/\N{NBSP}/, $title_string)
-or-
split(/\N{NO-BREAK SPACE}/, $title_string)

How to capitalize $2 in preg_replace?

I run a community site (built by hand using html, css, and php) for my wow guild. Recently, my members have demanded the ability to use tags like #username in their posts, which would then link to the users page. I already have this script, as listed below:
<?php
$string = '#user really?';
if (preg_match('/(^|\s)#([a-z0-9_]+)/i', $string))
{
$string = preg_replace('/(^|\s)#([a-z0-9_]+)/i', '#$2', $string);
echo $string;
}
else {
echo $string;
}
?>
This works fine, and I notice that the $2 is the user's name, but, since all user's names start with capital letters, the $2 needs to be capitalized, and not everyone does that. Is there a way to code it to automatically capitalize $2, or should I just tell users to remember to capitalize?
Thanx in advance,
Steven
use ucfirst ($2) to capitalize the first character
and strtoupper($2) if you want it all capital
I added more preg_replace and it now links to user no matter how u type name, as long as u have # symbol (#user, #USER, #User, #uSER, etc...). for anyone needing my solution, I will post it below:
<?php
$proper_str = "going to battle with #ADROK";
$proper_str = ucfirst(strtolower($proper_str));
$proper_str = preg_replace('/#[a-z]/e', 'ucfirst(strtoupper("$0"))', $proper_str);
$proper_str = preg_replace('/(^|\s)#([a-z0-9_]+)/i', ' #$2', $proper_str);
echo $proper_str;
?>
and if you change somethings around and make it:
<?php
$proper_str = "going to battle with e#mail.com";
if (preg_match('/(^|\s)#([a-z0-9_]+)/i', $proper_str))
{
$proper_str = ucfirst(strtolower($proper_str));
$proper_str = preg_replace('/#[a-z]/e', 'ucfirst(strtoupper("$0"))', $proper_str);
$proper_str = preg_replace('/(^|\s)#([a-z0-9_]+)/i', ' #$2', $proper_str);
echo $proper_str;
} else {
$proper_str = ucfirst("$proper_str");
echo $proper_str;
}
?>
Then it will leave email addresses alone and make sure first letter of post is capitalized!

Twitter Search API - PHP multiple word search not working

I have a simple search bar, user types in a search terms and tweets that match the search terms should show up. However, it only works for single word searches.
<div id="search">
<form action="" method="get">
<label>
Search:
<input type="text" name="search_box" id="search_box" />
<input type="submit" name="submit" id="submit" value="Search" />
</label>
</form>
</div>
<?php
$q=$_GET['search_box'];
$search = "http://search.twitter.com/search.atom?q=".$q."";
$tw = curl_init();
curl_setopt($tw, CURLOPT_URL, $search);
curl_setopt($tw, CURLOPT_RETURNTRANSFER, TRUE);
$twi = curl_exec($tw);
$search_res = new SimpleXMLElement($twi);
echo "<h3>Twitter search results for '".$q."'</h3>";
foreach ($search_res->entry as $twit1) {
$description = $twit1->content;
$message = $row['content'];
echo "<div class='text'>".$description."</div><br><br>";
}
curl_close($tw);
?>
If you're searching with quotation marks for exact phrase matches then you need to escape these quotes before constructing your query string. With the current code:
$q = $_GET['q']
$search = "http://search.twitter.com/search.atom?q=".$q."";
the input "test phrase" will result in this expression (which will probably cause a parse error):
"http://search.twitter.com/search.atom?q="test phrase"";
Instead, try urlencoding the user input before including it in the search string:
$q = urlencode($_GET['q']);
$search = "http://search.twitter.com/search.atom?q={$q}";
Which should result in the value:
http://search.twitter.com/search.atom?q=%22test%20phrase%22
You can check that this works by entering the URL in your browser.
Edit:
Here's the best resource for the Twitter Search API: https://dev.twitter.com/docs/using-search
For matching exact phrases, simply wrap the query string in qoutes.
--
In the Twitter API, any phrases are logicals ANDs, while comma separated values are logical ORs.
"Comma separated keywords and phrases are logical ORs, phrases are logical ANDs.
Words within phrases are delimited by spaces. A tweet matches if any phrase matches.
A phrase matches if all of the words are present in the tweet. (e.g. the twitter is the
AND twitter, and the,twitter is the OR twitter.). Terms are exact-matched, and also
exact-matched ignoring punctuation."
This is actually from the Streaming API but I believe that it also applied to the search API (somebody please correct me if I'm wrong!).
Let's consider what this means for a trivial example:
Tweet: "This is a tweet about grapefruit"
Will be matched with query:
tweet
grapefruit
tweet grapefruit
but not with:
pineapple
grapefruit pineapple
If you were searching for tweets wither either "pineapple" or "grapefruit" in, then your query parameter would need to be:
"grapefruit,pineapple".
$q = $_GET['search_box']." is";
$search = "http://search.twitter.com/search.atom?q=%22".urlencode( $q )."%22";

Zend PDF charset - croatian chartchters not suported - tried everything please HELP

Here is my code to create PDF document I cant see č ć ž š đ i tried importing .TTF file but can t import and use ttf properly please help
$pdf = new Zend_Pdf();
// Add new page to the document
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
// Draw something on a page
// Set font
$page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER), 20);
///tried to import TTF not working
//$goodDogCoolFont = Zend_Pdf_Font::fontWithPath('dokumenti/cro.TTF');
//$page->setFont($goodDogCoolFont, 36);
// Draw text
#
$page->setFillColor(Zend_Pdf_Color_Html::color('#990000'));
$page->drawText('Račćšđžčun za apartman AID '.$this->ukupnacjena[1]['AID'] , 10, 800, 'Windows-1250');// UTF-8 Also doesnt work
pdfData = $pdf->render();
$filename = $this->ukupnacjena[1]['OD-DO'];
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'.$filename.'.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
echo $pdfData;
I was facing similar issues with German Characters and found solution by replacing such characters[UMLAUTS in my situation] with its equivalent HTML Code like :
$str='German chars ü ä ö';
$str = html_entity_decode($str, ENT_COMPAT, "UTF-8");
$page->drawText($str, 115, 524,"UTF-8");
Will Print
German chars ü ä ö
May help you.....
I can see similar problems on this post. It i s actually a import problem.
Unicode characters not showing in Zend_Pdf?