我有一项任务是从一段文字中删除一部分字符串。这个概念如下。
1)应删除文本段落中括号“(”和“)”中字符串的部分。
2)然后,如果左括号距原始字符串的开头不超过30个字符,则不删除。
$sample_string = 'Pete Fountain (born Pierre Dewey LaFontaine, Jr., July 3, 1930), is an American, New Orleans based clarinetist. According to a Belgian radio program ("La troisieme oreille", produced by Marc Danval), his name was originally Pierre de la Fontaine.';
$output_string = 'Pete Fountain, is an American, New Orleans based clarinetist. According to a Belgian radio program ("La troisieme oreille", produced by Marc Danval), his name was originally Pierre de la Fontaine.'
虽然右括号“)”超出了字符串的第30个字符,但仍然必须删除字符串的部分。
括号内的许多文本都可以以第30个字符显示。
答案 0 :(得分:1)
看起来这可能是一项任务,所以我不想直截了当地回答......但我会给你一些指示。
如果你看一下这里:http://uk3.php.net/manual/en/function.stripos.php你会找到一个函数,它允许你在一个字符串中找到一个字符的出现。
此功能:http://uk3.php.net/substr将允许您从字符串中获取内容。
而且:http://uk1.php.net/strlen会让你获得字符串长度。
阅读PHP文档,玩游戏,看看你能想出什么。
答案 1 :(得分:1)
但是我同意Richard Simpson的观点,我想我们只需要给你一个有效的例子。 这样你下次就可以自己做(或至少学点东西)。
$sample_string = 'Pete Fountain (born Pierre Dewey LaFontaine, Jr., July 3, 1930), is an American, New Orleans based clarinetist.
According to a Belgian radio program ("La troisieme oreille", produced by Marc Danval), his name was originally Pierre de la Fontaine.';
$output_string = 'Pete Fountain, is an American, New Orleans based clarinetist.
According to a Belgian radio program ("La troisieme oreille", produced by Marc Danval), his name was originally Pierre de la Fontaine.';
echo '<b>Original string</b>'. "<br>";
echo $sample_string;
echo "<br>";
if (strpos(substr($sample_string, 0, 30), '(') !== false) {
$newString = preg_replace('/\(.*\)/', '', $sample_string, 1);
echo "<b>New string</b>" . "<br>";
echo $newString;
echo "<br>";
}
echo "<b>Needed output</b>" . "<br>";
echo $output_string;
strpos
用于检查字符串的前30个字符内是否有'('。
preg_replace
替换了第一个'('到第一个')'字符中的所有字符。
输出:
原始字符串 Pete Fountain(1930年7月3日出生于Pierre Dewey LaFontaine,是一位美国新奥尔良的单簧管演奏家)。根据比利时广播节目(由Marc Danval制作的“La troisieme oreille”),他的名字最初是皮埃尔·德拉方丹。
新字符串 Pete Fountain,是一位美国人,新奥尔良的单簧管演奏家。根据比利时广播节目(由Marc Danval制作的“La troisieme oreille”),他的名字最初是皮埃尔·德拉方丹。
需要的输出 Pete Fountain,是一位美国人,新奥尔良的单簧管演奏家。根据比利时广播节目(由Marc Danval制作的“La troisieme oreille”),他的名字最初是皮埃尔·德拉方丹。
答案 2 :(得分:1)
这是你的函数,它也适用于另一个括号内的括号,例如“一个字符串(一些字符串(多一些)一些文本)”。
$ distance是您希望左括号离开头的字符数(在您的情况下为30)
function remove($str, $distance) {
$arr = explode('(', $str);
// if first opening paranthesis is not farther away from distance or if there are no
// parentheses at all, just return the same string
if (strlen($arr[0]) >= $distance || count($arr) === 1) {
return $str;
}
// get the position of first character after the first opening parenthesis
$currentPos = strlen($arr[0])+1;
$open = '(';
$close = ')';
$openCount = 1;
// loop through each character to find the matching closing parenthesis
while ($currentPos < strlen($str)) {
$char = substr($str, $currentPos,1);
// if we find another opening parenthesis, increase the opening parentheses count
if ($char == $open) {
$openCount++;
}
// if we find a closing parenthesis, decrease openening parentheses count
if ($char == $close) {
$openCount--;
}
// when opening parentheses count is zero, that means we found closing parenthesis
// position, so now we can remove the substring between opening and closing
// parentheses, including the opening and closing parentheses, and return the
// result
if ($openCount === 0) {
$parens = substr($str, strlen($arr[0]), $currentPos - strlen($arr[0])+1);
return str_replace($parens, '', $str);
}
$currentPos++;
}
return $str;
}
答案 3 :(得分:0)
试试这个
<?php
$sample_string = 'Pete Fountain (born Pierre Dewey LaFontaine, Jr., July 3, 1930), is an American, New Orleans based clarinetist.
According to a Belgian radio program ("La troisieme oreille", produced by Marc Danval), his name was originally Pierre de la Fontaine.';
$first_limit='(';
$second_limit=')';
$further=30;
$replace=' ';
echo remove_blablabla_string($sample_string,$first_limit,$second_limit,$further,$replace);
/* FUNCTION */
function remove_blablabla_string($string,$limit1,$limit2,$max_further,$replace_by){
$find=true;
$original_string=$string;
do{
$string_between=between($string,$limit1,$limit2);
if($string_between==false)
$find=false;
else if(strpos($original_string,$string_between)<=$max_further){
$string=str_replace($limit1.$string_between.$limit2,$replace_by,$string);
}else{
$find=false;
}
}while($find);
return $string;
}
function between($string,$limit1,$limit2){
$part1=explode($limit1,$string);
if(!isset($part1[1]))
return false;
$part2=explode($limit2,$part1[1]);
if(!isset($part2[1]))
return false;
return $part2[0];
}