类似SQL的东西;例如;但是在PHP中
我的数据库中几乎没有url,如下所示: id url类似SQL的东西;例如;但是在PHP中,php,Php,我的数据库中几乎没有url,如下所示: id url 1 http://test.com/embed-990.html 2. http://test2.com/embed-011.html 3. http://test3.com/embed-022.html 若其中一个url不存在于数据库中,我如何制作一个简单的php代码来加载另一个url?我还需要按域检查这些url 例如,类似这样的事情: if($data['url'] == "test.com") { echo "my embed co
1 http://test.com/embed-990.html
2. http://test2.com/embed-011.html
3. http://test3.com/embed-022.html
若其中一个url不存在于数据库中,我如何制作一个简单的php代码来加载另一个url?我还需要按域检查这些url
例如,类似这样的事情:
if($data['url'] == "test.com") {
echo "my embed code number 1";
} elseif($data['url'] == "test2.com") {
echo "my another embed code";
}
您可以获取主机,然后进行比较
$dataurl = array('http://test.com/embed-990.html',
'http://test2.com/embed-011.html',
'http://test3.com/embed-022.html');
foreach($dataurl as $url) {
switch(parse_url($url, PHP_URL_HOST)) {
case 'test.com':
echo 'test domain';
break;
case 'test2.com':
echo 'test domain 2';
break;
default:
echo 'unknown';
break;
}
echo $url . PHP_EOL;
}
演示:
对于类似SQL“like”的问题,可以在preg\u match
中使用正则表达式 您可以获取主机,然后进行比较
$dataurl = array('http://test.com/embed-990.html',
'http://test2.com/embed-011.html',
'http://test3.com/embed-022.html');
foreach($dataurl as $url) {
switch(parse_url($url, PHP_URL_HOST)) {
case 'test.com':
echo 'test domain';
break;
case 'test2.com':
echo 'test domain 2';
break;
default:
echo 'unknown';
break;
}
echo $url . PHP_EOL;
}
演示:
对于类似SQL“like”的问题,可以在
preg\u match
中使用正则表达式 您可以使用substr\u count
if (substr_count($data['url'], 'test.com') > 0) {
echo "my embed code number 1";
}
else if (substr_count($data['url'], 'test2.com') > 0) {
echo "my embed code number 2";
}
或strpos
if (strpos($data['url'],'test.com') !== false) {
echo "my embed code number 1";
}
else if (strpos($data['url'],'test2.com') !== false) {
echo "my embed code number 2";
}
或preg\u match
if(preg_match('/test.com/',$data['url']))
{
echo "my embed code number 1";
}
else if(preg_match('/test2.com/',$data['url']))
{
echo "my embed code number 2";
}
您可以使用
substr\u count
if (substr_count($data['url'], 'test.com') > 0) {
echo "my embed code number 1";
}
else if (substr_count($data['url'], 'test2.com') > 0) {
echo "my embed code number 2";
}
或strpos
if (strpos($data['url'],'test.com') !== false) {
echo "my embed code number 1";
}
else if (strpos($data['url'],'test2.com') !== false) {
echo "my embed code number 2";
}
或preg\u match
if(preg_match('/test.com/',$data['url']))
{
echo "my embed code number 1";
}
else if(preg_match('/test2.com/',$data['url']))
{
echo "my embed code number 2";
}
您可以使用Regx
$domains = ['test.com', 'test1.com', 'test20.com'];
foreach( $domains as $domain ){
if(preg_match('/test([0-9]*)\.com/', $domain, $match)){
echo "my embed code number {$match[1]}\n";
}
}
产出:
my embed code number
my embed code number 1
my embed code number 20
你可以在这里测试
对于regx
按字面意思匹配test
test
-捕获组,匹配([0-9]*)
一次或多次0-9
按字面意思匹配\。
按字面意思匹配com
com
*
放在捕获组([0-9])*
之外将匹配并通过if,但不会捕获捕获组内的任何内容。这是有道理的,但值得注意的是,您将得到以下信息:
注意:第6行的未定义偏移量:1 in[…][…]
对于test.com
如果要匹配embed-
中的数字,可以使用其中一个
'/test\.com\/embed-([0-9]{3})\.html/'
'/\/embed-([0-9]{3})\.html/'
'/\/embed-([0-9]{3})\./'
取决于你想要的具体程度。您可以在此页面上使用不同的Regx
正则表达式非常强大,它们用于模式匹配,这正是您所需要的
干杯。您可以使用Regx
$domains = ['test.com', 'test1.com', 'test20.com'];
foreach( $domains as $domain ){
if(preg_match('/test([0-9]*)\.com/', $domain, $match)){
echo "my embed code number {$match[1]}\n";
}
}
产出:
my embed code number
my embed code number 1
my embed code number 20
你可以在这里测试
对于regx
按字面意思匹配test
test
-捕获组,匹配([0-9]*)
一次或多次0-9
按字面意思匹配\。
按字面意思匹配com
com
*
放在捕获组([0-9])*
之外将匹配并通过if,但不会捕获捕获组内的任何内容。这是有道理的,但值得注意的是,您将得到以下信息:
注意:第6行的未定义偏移量:1 in[…][…]
对于test.com
如果要匹配embed-
中的数字,可以使用其中一个
'/test\.com\/embed-([0-9]{3})\.html/'
'/\/embed-([0-9]{3})\.html/'
'/\/embed-([0-9]{3})\./'
取决于你想要的具体程度。您可以在此页面上使用不同的Regx
正则表达式非常强大,它们用于模式匹配,这正是您所需要的
干杯。我不明白精确匹配的目的(你可以用更少的开销来做
=
),你的语法是错误的,因为你需要在Regx中转义
,否则它将匹配该位置的任何字符,例如/test2.com/
匹配test2 com/
请参见:是的,您是对的,但我尝试建议可以用作这一点的函数。对于正则表达式中的精确匹配,应仅使用test1或test2,但我认为这不是实际文本来检查。是的,但从逻辑上讲,这些函数是等效的if(preg_match(“/test\.com/”、$data['url'])
和if('test.com'==$data['url])
,它们无法显示模式匹配的“威力”。我猜是“技术上”你可以争辩说这个/^test\.com$/
是等价的,因为你的匹配是sometest.com
,但你的答案并不清楚。我不明白精确匹配的目的(你可以用更少的开销来做=
),您的语法是错误的,因为您需要转义Regx中的
,否则它将匹配该位置的任何字符,例如/test2.com/
匹配test2.com/
请参见:是的,您是对的,但我尝试建议可以用作此点的函数。对于regex中的精确匹配,应仅使用test1或test2,但我认为这不是要检查的实际文本。是的,但从逻辑上讲,它们是等价的if(preg_match('/test\.com/,$data['url'))
和if('test.com'=$data['url])
,它们无法显示模式匹配的“威力”。我猜“技术上”你可以说这个/^test\.com$/
是等价的,因为你的将匹配sometest.com
,但你的答案并不清楚。