English 中文(简体)
消除插图中变数的重复?
原标题:Removing a duplication of a variable in a string?
  • 时间:2010-11-17 10:12:52
  •  标签:
  • php

你们有:

  $string =  hello my name is blah blah and wats yours ;

你们想到并检查任何可能出现重复的地方,而不是任何字眼,而只是这里选择的字眼。

   $variable =  blah ;

Ie,如果防弹背后,就去除其中一人。

我认为,将阵列分为一个阵列,如果阵列中一个变数以同一字开始,最后一个字则冲淡一个阵列并重新铺设。 顺便提一下,这就是我为什么要问,可能有一个更简单的方式。

Any ideas?

edit: i Just realised i don t consider do a preg_match of only blah blah with blah .

最佳回答

如何做到这一点:

$string =  hello my name is blah blah and wats yours ;
$variable =  blah ;
$string = preg_replace(  /( .$variable. s+){2,}/  ,  1  , $string );

允许处理一个以上变量(例如,你可以通过若干<条码>和可变<>代码>,或者你可以创建一系列变量,使用一个<条码>前_replace()。

或仅使用<代码>str_replace()

$string =  hello my name is blah blah and wats yours ;
$variable =  blah ;
$string = str_replace( $variable.   .$variable.    , $variable.    , $string );
问题回答

您可以检查每张美元舱位的变动情况,并检查几个位置是否在可变的美元长度(也许会+白色空间)上有所区别。

$string =  hello my name is blah blah and wats yours ;
$search_string =  blah ;

$first_occurence = strpos($string , $search_string);

If (int preg_match($search_string , $string ) > 1) {
    echo "String found more than once!!!";

    // remove all occurences of searchstring from string excepte the first one
    $string = substr ($string, 0, $first_occurence +1) . str_replace($search_string,   , substr ($string,$first_occurence +1));
}

到目前为止,所有答复都谈到在扼杀词上的任何重复。 我认为,你只是想删除连续的同样措辞。 可在preg_replace上填写:

$string =  hello my name is blah blah and wats yours ;

$string = preg_replace( /(w+)(s+)\1s*/ ,  \1\2 , $string);

指出这一职能相当缓慢,因此将删除诸如“哈德”之类的有效短语。 也许你可以通过一个白色名单和preg_replace_questback来围绕这一点开展工作。


仅重读你的问题,以前就错过了“只字”的界限。 你可以用黑字清单来做到这一点,但绝不能重复:

$string =  hello my name is blah blah and wats yours. I had had a bad day ;

$string = preg_replace_callback( /(w+)(s+)\1s*/i , function($matches) {
    $blacklist = array ( blah );

    if (in_array(strtolower($matches[1]), $blacklist)) {
        return $matches[1] . $matches[2];
    } else {
        return $matches[0];
    }
}, $string);

// $string == "hello my name is blah and wats yours. I had had a bad day"

您可在<代码>黑名单/代码>上添加一个以上字。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签