Case-insensitive PSpell spell checking? - php

I'd like to use PHP's PSpell check function in my program. Is there an option somewhere for case-insensitive checking in pspell_check()?

I've found a way around the lack of an option for case insensitivity. PSpell's suggestion function seems to always return the correct capitalization of a mis-capitalized word as its first suggestion, so we can check for this if the initial spell check fails:
<?php
function pspell_icheck($dictionary_link, $word) {
return ( pspell_check($dictionary_link, $word) ||
strtolower(reset(pspell_suggest($dictionary_link, $word))) == strtolower($word) );
}
$dict = pspell_new('en');
$word = 'foo';
echo pspell_icheck($dict, $word);
?>
Works on PHP 5.3.2. Happy coding :)

Try this patch http://code.google.com/p/patched-pspell/ . It enables you to set any options.
pspell_config_set($pspell_config, 'ignore-case', 'true');

There is an easy solution. Just do this:
$word = ucfirst($word); //Always capitalize to avoid case sensitive error
if (!pspell_check($dict, $word)) {
$suggestions = pspell_suggest($dictionary, $word);
}

Related

PHP: How to make INVOKING internal functions faster

I am trying to optimize a function that is called 560x during the execution of a request.
The function is this (some info redacted):
function foo($text) {
if (preg_match('/[`"]/', $text)) {
throw new Exception("very detailed error");
}
$text = explode('.', $text);
$tick = '*';
return $tick . implode("$tick.$tick", $text) . $tick;
}
Using XDEBUG PHP profiler + Webgrind, I see that these stats (in milliseconds) for this function:
As you can see, only 3ms is spent in preg_match, explode, implode combined. The other 20ms is spent just invoking these functions and concatenating some string. Is there a way I can reduce the total self cost of this function?
I have tried adding
use function preg_match;
use function explode;
use function implode;
as suggested here, but I did not see any improvements. Any suggestions?
Consider switching to a more light-weight functions (but maybe losing some readability):
function foo($text) {
if (strpos($text, '"')!==FALSE || strpos($text, '`')!==FALSE) {
throw new Exception("very detailed error");
}
$tick = '*';
return $tick . str_replace('.', "$tick.$tick", $text) . $tick;
}

Url validation with preg_match

I want to make sure the link is from the chosen social type.
help!!
$socialType = 'youtube';
$link = 'https://www.youtube.co.uk/watch?v=DBK-Cy9ge4M';
if (!preg_match("/^(http|https):\\/\\/[a-z0-9_]+$socialType*\\.[_a-z]{2,5}"."((:[0-9]{1,5})?\\/.*)?$/i",$link))
{
return Response::json('inValid');
}
{
return Response::json('Valid');
}
There will be two options as-
1. with preg_match -
$subject = "https://www.youtube.co.uk/watch?v=DBK-Cy9ge4M";
$pattern = '/^youtube/';
preg_match($pattern, substr($subject,7), $matches);
print_r($matches);
2. with strops as (Ruslan Osmanov)-
$socialType = 'youtube';
$link = 'https://www.youtube.co.uk/watch?v=DBK-Cy9ge4M';
if (strpos($link, $socialType) !== false) {
return Response::json('Valid');
}
You can simply check, if the link contains your substring using strpos:
$link = 'https://www.youtube.co.uk/watch?v=DBK-Cy9ge4M';
$type = 'youtube';
if (strpos($link, $type) !== false) {
// passed
}
Or use a simple regular expression, if you want stricter check:
$reg_type = preg_quote($type, '/');
if (preg_match("/^https?:\/\/(www\.)?$reg_type/", $link)) {
// passed
}
Note, you should escape values passed into the regular expression using preg_quote.
The pattern should be just enough. Don't overcomplicate. It's generally impossible to write a perfect regular expression. For example, it is very unlikely to find HTTP(S) protocol prefix + optional "www." + "youtube." in an URL not belonging to Youtube.
Also, I wouldn't expect to get the answer with a universal regular expression for all kinds of social networks. Each has its own pattern.

PHP - How do I check if a string contains a word?

Good evening.
I'm making an IRC bot that responds when you mention him. What I want to know is how to make him reply when someone actually says his name. This is what I have so far ($match[3] is the message that someone said on a channel and yes, stripos is because I want it case-insensitive ):
if (stripos($match[3], "ircBot") !== false) {
$isMentioned = true;
}else { $isMentioned = false; }
while this does in fact detect if someone said his name, it only works if he's mentioned at the very beginning of the message so for example:
"ircBot is at the beginning of this sentance" would make $isMentioned true
"There's ircBot in between this sentance" would make $isMentioned false
"At the end of this sentance is ircBot" would make $isMentioned false
I want it to return true if "ircBot" is anywhere inside $match[3] and not just the beginning
You have to look for word boundaries to avoid someone called MircBot
// using in_array
$isMentioned = in_array('ircbot', preg_split('/\s+/', mb_strtolower($match[3])));
// using regex word boundaries
$isMentioned = preg_match('/\b(ircBot)\b/i', $match[3]);
http://3v4l.org/lh3JT
Use stristr instead
if (stristr($match[3], "ircBot") !== false) {
$isMentioned = true;
}else { $isMentioned = false; }
I think your error is somewhere else, e.g. the construction of $match[3]. This works fine:
$isMentioned = stripos('This is in the middle of ircBot the string','ircbot') !== false;
echo( $isMentioned ? 'Is Mentioned' : 'Sad ignored bot');

Reading an email's subject in Unicode out of an IMAP server

I'm using Zend Framework 1's IMAP server connector and I'm trying to fetch an email from server with Unicode characters in its subject. Here's how I do it:
$message = $imapServer->getMessage($message_number);
echo $message->getHeader('subject');
The problem is that it comes out encoded:
=?UTF-8?B?2KjYp9uM?=
I can find the encoding function within Zend_Mail class named _encodeHeader but I can not find the decoding pair! Does anyone know how to decode this string?
And here's the encoder function:
protected function _encodeHeader($value)
{
if (Zend_Mime::isPrintable($value) === false) {
if ($this->getHeaderEncoding() === Zend_Mime::ENCODING_QUOTEDPRINTABLE) {
$value = Zend_Mime::encodeQuotedPrintableHeader($value, $this->getCharset(), Zend_Mime::LINELENGTH, Zend_Mime::LINEEND);
} else {
$value = Zend_Mime::encodeBase64Header($value, $this->getCharset(), Zend_Mime::LINELENGTH, Zend_Mime::LINEEND);
}
}
return $value;
}
Search for a "RFC2047 decoder" and pick one of the existing libraries which does just that. If nothing is usable, roll your own.
Here's how I solved it:
switch (strtolower($encoding)) {
case \Zend_Mime::ENCODING_QUOTEDPRINTABLE:
if (preg_match('/^\s?=\?([^\?]+)\?Q\?/', $str, $matches) === 1) {
$str = preg_replace('/\s?=\?'.preg_quote($matches[1]).'\?Q\?/', ' ', $str);
$str = strtr($str, array('?=' => ''));
$str = trim($str);
}
return \Zend_Mime_Decode::decodeQuotedPrintable($str);
case \Zend_Mime::ENCODING_BASE64:
return base64_decode($encodedText);
case \Zend_Mime::ENCODING_7BIT:
case \Zend_Mime::ENCODING_8BIT:
default:
return $encodedText;
}

Get youtube id for all url types

The following code works with all YouTube domains except for youtu.be. An example would be: http://www.youtube.com/watch?v=ZedLgAF9aEg would turn into: ZedLgAF9aEg
My question is how would I be able to make it work with http://youtu.be/ZedLgAF9aEg.
I'm not so great with regex so your help is much appreciated. My code is:
$text = preg_replace("#[&\?].+$#", "", preg_replace("#http://(?:www\.)?youtu\.?be(?:\.com)?/(embed/|watch\?v=|\?v=|v/|e/|.+/|watch.*v=|)#i", "", $text)); }
$text = (htmlentities($text, ENT_QUOTES, 'UTF-8'));
Thanks again!
//$url = 'http://www.youtube.com/watch?v=ZedLgAF9aEg';
$url = 'http://youtu.be/ZedLgAF9aEg';
if (FALSE === strpos($url, 'youtu.be/')) {
parse_str(parse_url($url, PHP_URL_QUERY), $id);
$id = $id['v'];
} else {
$id = basename($url);
}
echo $id; // ZedLgAF9aEg
Will work for both versions of URLs. Do not use regex for this as PHP has built in functions for parsing URLs as I have demonstrated which are faster and more robust against breaking.
Your regex appears to solve the problem as it stands now? I didn't try it in php, but it appears to work fine in my editor.
The first part of the regex http://(?:www\.)?youtu\.?be(?:\.com)?/matches http://youtu.be/ and the second part (embed/|watch\?v=|\?v=|v/|e/|.+/|watch.*v=|) ends with |) which means it matches nothing (making it optional). In other words it would trim away http://youtu.be/ leaving only the id.
A more intuitive way of writing it would be to make the whole if grouping optional I suppose, but as far as I can tell your regex is already solving your problem:
#http://(?:www\.)?youtu\.?be(?:\.com)?/(embed/|watch\?v=|\?v=|v/|e/|.+/|watch.*v=)?#i
Note: Your regex would work with the www.youtu.be.com domain as well. It would be stripped away, but something to watch out for if you use this for validating input.
Update:
If you want to only match urls inside [youtube][/youtube] tags you could use look arounds.
Something along the lines of:
(?<=\[youtube\])(?:http://(?:www\.)?youtu\.?be(?:\.com)?/(?:embed/|watch\?v=|\?v=|v/|e/|[^\[]+/|watch.*v=)?)(?=.+\[/youtube\])
You could further refine it by making the .+ in the look ahead only match valid URL characters etc.
Try this, hope it'll help you
function YouTubeUrl($url)
{
if($url!='')
{
$newUrl='';
$videoLink1=$url;
$findKeyWord='youtu.be';
$toBeReplaced='www.youtube.com';
if(IsContain('watch?v=',$videoLink1))
{
$newUrl=tMakeUrl($videoLink1);
}
else if(IsContain($videoLink1, $findKeyWord))
{
$videoLinkArray=explode('/',$videoLink1);
$Protocol='';
if(IsContain('://',$videoLink1))
{
$protocolArray=explode('://',$videoLink1);
$Protocol=$protocolArray[0];
}
$file=$videoLinkArray[count($videoLinkArray)-1];
$newUrl='www.youtube.com/watch?v='.$file;
if($Protocol!='')
$newUrl.=$Protocol.$newUrl;
else
$newUrl=tMakeUrl($newUrl);
}
else
$newUrl=tMakeUrl($videoLink1);
return $newUrl;
}
return '';
}
function IsContain($string,$findKeyWord)
{
if(strpos($string,$findKeyWord)!==false)
return true;
else
return false;
}
function tMakeUrl($url)
{
$tSeven=substr($url,0,7);
$tEight=substr($url,0,8);
if($tSeven!="http://" && $tEight!="https://")
{
$url="http://".$url;
}
return $url;
}
You can use bellow function for any of youtube URL
I hope this will help you
function checkYoutubeId($id)
{
$youtube = "http://www.youtube.com/oembed?url=". $id ."&format=json";
$curl = curl_init($youtube);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);
return json_decode($return, true);
}
This function return Youtube video detail if Id match to youtube video ID
A little improvement to #rvalvik answer would be to include the case of the mobile links (I've noticed it while working with a customer who used an iPad to navigate, copy and paste links). In this case, we have a m (mobile) letter instead of www. Regex then becomes:
#(https?://)?(?:www\.)?(?:m\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x
Hope it helps.
A slight improvement of another answer:
if (strpos($url, 'feature=youtu.be') === TRUE || strpos($url, 'youtu.be') === FALSE )
{
parse_str(parse_url($url, PHP_URL_QUERY), $id);
$id = $id['v'];
}
else
{
$id = basename($url);
}
This takes into account youtu.be still being in the URL, but not the URL itself (it does happen!) as it could be the referring feature link.
Other answers miss out on the point that some youtube links are part of a playlist and have a list paramater also which is required for embed code. So to extract the embed code from link one could try this JS code:
let urlEmbed = "https://www.youtube.com/watch?v=iGGolqb6gDE&list=PL2q4fbVm1Ik6DCzm9XZJbNwyHtHGclcEh&index=32"
let embedId = urlEmbed.split('v=')[1];
let parameterStringList = embedId.split('&');
if (parameterStringList.length > 1) {
embedId = parameterStringList[0];
let listString = parameterStringList.filter((parameterString) =>
parameterString.includes('list')
);
if (listString.length > 0) {
listString = listString[0].split('=')[1];
embedId = `${parameterStringList[0]}?${listString}`;
}
}
console.log(embedId)
Try it out here: https://jsfiddle.net/AMITKESARI2000/o62dwj7q/
try this :
$string = explode("=","http://www.youtube.com/watch?v=ZedLgAF9aEg");
echo $string[1];
would turn into: ZedLgAF9aEg

Categories