不区分大小写“减去”另一个文本(php)

时间:2010-06-24 12:25:16

标签: php string

(抱歉!有关$字符的修订。也可以通过示例解释)

我需要对两个字符串变量进行测试:

$ a,$ b

如果$ b中包含$ a,我需要查看它们的“差异”($ b- $ a)是否包含$字符。如果是,则返回“差异”,否则在所有其他情况下,测试应返回FALSE

$ a也应该只出现在$ b的结尾...

$ b =“abcde” $ a =“cde” 应该导致true(没有检测到$)

$ b =“zyw $ abcde” $ a =“cde” 应导致错误($检测到)

$ b =“cdeab” $ a =“cde” 应该导致错误(不在最后)

$ b =“zxwq” $ a =“cde” 应该导致错误($ a不存在于$ b)

HTH!

4 个答案:

答案 0 :(得分:1)

  • 使用strpos确定$ b in $ b
  • 的位置
  • 使用substr从$ b获取不包含$ a
  • 的部分
  • 使用strpos检查这些部分是否包含$。

答案 1 :(得分:0)

修改:问题编辑后更新

这应该有效:

<?php

function string_func_thing($a, $b) {
    $count = 0;

    //get $b with $a removed
    $result = str_ireplace($a, '', $b, $count);   

    //if $a was not in $b
    if (!$count) {
        return false;
    }
    //if whatever is left contains $
    if (false !== strpos($result, '$')) {
        return false;
    }

    return true;
}


var_dump( string_func_thing('ter', 'computer$') ); //bool(false) 
var_dump( string_func_thing('ter', 'computer') ); //bool(true) 
var_dump( string_func_thing('hello', 'computer') ); //bool(true) 

答案 2 :(得分:0)

$ret = false;
if (strpos($b, $a) !== false) {
    $ret = (substr_count($b) > substr_count($a));
}

$ ret是你的答案

答案 3 :(得分:0)

$a = 'cde';
$regex = '/^[^$]*' . $a . '$/m';
if (preg_match($regex, $b)) {
    # Successful match
} else {
    # Match attempt failed
}