如果字符串包含多个特定单词,如何检查?
我可以使用以下代码检查单个单词:
$data = "text text text text text text text bad text text naughty";
if (strpos($data, 'bad') !== false) {
echo 'true';
}
Run Code Online (Sandbox Code Playgroud)
但是,我想添加更多单词来检查.像这样的东西:
$data = "text text text text text text text bad text text naughty";
if (strpos($data, 'bad || naughty') !== false) {
echo 'true';
}
?>
Run Code Online (Sandbox Code Playgroud)
(如果找到任何这些单词,那么它应该返回true)
但是,上面的代码无法正常工作.任何想法,我做错了什么?
我正在使用这些代码来检查字符串是否为英文.
<?php
$string="?????????? (?";
if(!preg_match('/[^\W_ ] /',$string))
{
echo "Please enter English words only:(";
}
else {
echo "OK, English Detected!";
}
?>
Run Code Online (Sandbox Code Playgroud)
它不能提供完美的结果,因为像"some english text ?????????? (?"这样的字符串也检测为英语,任何想法?
我倾向于C编程.我写了一个奇怪的循环,但在我使用%c时不起作用scanf().
这是代码:
#include<stdio.h>
void main()
{
char another='y';
int num;
while ( another =='y')
{
printf("Enter a number:\t");
scanf("%d", &num);
printf("Sqare of %d is : %d", num, num * num);
printf("\nWant to enter another number? y/n");
scanf("%c", &another);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我%s在这段代码中使用scanf("%s", &another);,那么它工作正常.
为什么会这样?任何的想法?
要获取域名,我使用此代码:
<?php
$myURL = 'http://answers.yahoo.com/question/index?qid=20130406061745AAmovgl';
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
if (preg_match($pattern, $myURL, $domain) === 1) {
$domain = $domain[0];
}
$ndomain = "http://$domain";
echo $ndomain;
?>
Run Code Online (Sandbox Code Playgroud)
但它会输出:http://yahoo.com
但是,我如何http://answers.yahoo.com准确地输出这个子域.
我知道如何用C++定义一些东西.例如:
#define ce cout<<"\n";
Run Code Online (Sandbox Code Playgroud)
现在,如果我们使用ce它将输出的代码的任何位置,cout<<"\n";
我正在尝试使用此JavaSvript:
#define go document.write
Run Code Online (Sandbox Code Playgroud)
所以,如果我想显示/输出一些东西,我想写
go('Great!');
Run Code Online (Sandbox Code Playgroud)
现在,这将Great!在浏览器中输出.但是这种define方法在JavaScript中不起作用