Javascript 从文本中出现的字符串填充数组
我需要用文本中的所有“特定事件”字符串填充数组。假设我有文本:Javascript 从文本中出现的字符串填充数组,javascript,php,regex,laravel,Javascript,Php,Regex,Laravel,我需要用文本中的所有“特定事件”字符串填充数组。假设我有文本: “嗨,我叫鲍勃,我今年20岁,喜欢猫。[这里有很多短信]嗨,我叫鲍勃 迪伦,我今年25岁,喜欢狗。[这里有很多短信]嗨,我叫塔姆伯恩,我 我30岁了,喜欢海龟。[此处有大量文本] 所以我需要一个循环来搜索“Hi,mas name is”,并在句点/点之前获取信息。因此,我的输出类似于: 数组[0]->“嗨,我叫鲍勃,我今年20岁,喜欢猫。” 阵列[1]->“嗨,我叫迪伦,我今年20岁,喜欢狗。” 阵列[3]->“嗨,我的名字叫
- “嗨,我叫鲍勃,我今年20岁,喜欢猫。[这里有很多短信]嗨,我叫鲍勃 迪伦,我今年25岁,喜欢狗。[这里有很多短信]嗨,我叫塔姆伯恩,我 我30岁了,喜欢海龟。[此处有大量文本]
- 数组[0]->“嗨,我叫鲍勃,我今年20岁,喜欢猫。”
- 阵列[1]->“嗨,我叫迪伦,我今年20岁,喜欢狗。”
- 阵列[3]->“嗨,我的名字叫铃鼓,我今年20岁,喜欢海龟。”
$html = $file;
$needle = "\$table->";
$lastPos = 0;
$positions = array();
$positions2 = array();
while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
你可以用它来做这个
使用phpexplode()
:
$string = "Hi, my name is Bob, I am 20 years old and like cats. Hi, my name is Dylan, I am 25 years old and like dogs. Hi, my name is Tambourine, I am 30 years old and like turtles";
print_r (explode(".",$string));
$string = "Hi, my name is Bob, I am 20 years old and like cats. Hi, my name is Dylan, I am 25 years old and like dogs. Hi, my name is Tambourine, I am 30 years old and like turtles";
$arr = preg_split('/[ap]\.m\.(*SKIP)(*FAIL)|\./', $string);
print_r($arr);
使用正则表达式:
$string = "Hi, my name is Bob, I am 20 years old and like cats. Hi, my name is Dylan, I am 25 years old and like dogs. Hi, my name is Tambourine, I am 30 years old and like turtles";
print_r (explode(".",$string));
$string = "Hi, my name is Bob, I am 20 years old and like cats. Hi, my name is Dylan, I am 25 years old and like dogs. Hi, my name is Tambourine, I am 30 years old and like turtles";
$arr = preg_split('/[ap]\.m\.(*SKIP)(*FAIL)|\./', $string);
print_r($arr);
你可以用它来做这个
使用phpexplode()
:
$string = "Hi, my name is Bob, I am 20 years old and like cats. Hi, my name is Dylan, I am 25 years old and like dogs. Hi, my name is Tambourine, I am 30 years old and like turtles";
print_r (explode(".",$string));
$string = "Hi, my name is Bob, I am 20 years old and like cats. Hi, my name is Dylan, I am 25 years old and like dogs. Hi, my name is Tambourine, I am 30 years old and like turtles";
$arr = preg_split('/[ap]\.m\.(*SKIP)(*FAIL)|\./', $string);
print_r($arr);
使用正则表达式:
$string = "Hi, my name is Bob, I am 20 years old and like cats. Hi, my name is Dylan, I am 25 years old and like dogs. Hi, my name is Tambourine, I am 30 years old and like turtles";
print_r (explode(".",$string));
$string = "Hi, my name is Bob, I am 20 years old and like cats. Hi, my name is Dylan, I am 25 years old and like dogs. Hi, my name is Tambourine, I am 30 years old and like turtles";
$arr = preg_split('/[ap]\.m\.(*SKIP)(*FAIL)|\./', $string);
print_r($arr);
您可以使用此正则表达式捕获所有句子
[A-Z][^.]+\.?
试试这个PHP代码
$s = "Hi, my name is Bob, I am 20 years old and like cats. Hi, my name is Dylan, I am 25 years old and like dogs. Hi, my name is Tambourine, I am 30 years old and like turtles";
preg_match_all('/[A-Z][^.]+\.?/', $s, $matches);
print_r($matches);
印刷品
Array
(
[0] => Array
(
[0] => Hi, my name is Bob, I am 20 years old and like cats.
[1] => Hi, my name is Dylan, I am 25 years old and like dogs.
[2] => Hi, my name is Tambourine, I am 30 years old and like turtles
)
)
如果您是用Javascript编写的,下面是一个JS演示
var s=“嗨,我叫鲍勃,我20岁,喜欢猫。嗨,我叫迪伦,我25岁,喜欢狗。嗨,我叫塔姆伯恩,我30岁,喜欢海龟”
log(s.match(/[A-Z][^.]+\.?/g))
您可以使用此正则表达式捕获所有句子
[A-Z][^.]+\.?
试试这个PHP代码
$s = "Hi, my name is Bob, I am 20 years old and like cats. Hi, my name is Dylan, I am 25 years old and like dogs. Hi, my name is Tambourine, I am 30 years old and like turtles";
preg_match_all('/[A-Z][^.]+\.?/', $s, $matches);
print_r($matches);
印刷品
Array
(
[0] => Array
(
[0] => Hi, my name is Bob, I am 20 years old and like cats.
[1] => Hi, my name is Dylan, I am 25 years old and like dogs.
[2] => Hi, my name is Tambourine, I am 30 years old and like turtles
)
)
如果您是用Javascript编写的,下面是一个JS演示
var s=“嗨,我叫鲍勃,我20岁,喜欢猫。嗨,我叫迪伦,我25岁,喜欢狗。嗨,我叫塔姆伯恩,我30岁,喜欢海龟”
console.log(s.match(/[A-Z][^.]+\.?/g))
分享你的代码到目前为止你做了什么对不起,完成了。谢谢你分享你的代码你到目前为止做了什么对不起,完成了。谢谢,也许我不能使用php explode,因为我只写了部分文本,在“点/句点”之后还有更多文本。但我只是用正则表达式测试它,它正是我所需要的。真的谢谢你。现在我需要学习正则表达式以便完成它,但到目前为止它是完美的。@JeúCasulo欢迎,继续编码:)顺便说一句,我为Pushpesh Kumar Rajwanshi给出了“正确的答案”,因为他是第一个。但我真的很感谢你you@JeúCasulo不担心broMaybe我不能使用php爆炸,因为我只写了部分文本,在“点/句点”之后还有更多文本。但我只是用正则表达式测试它,它正是我所需要的。真的谢谢你。现在我需要学习正则表达式以便完成它,但到目前为止它是完美的。@JeúCasulo欢迎,继续编码:)顺便说一句,我为Pushpesh Kumar Rajwanshi给出了“正确的答案”,因为他是第一个。但我真的很感谢你you@JeúCasulo不担心broMaybe我不能使用php split,因为我只写了部分文本,在“点/句点”之后还有更多文本。但我用regex anwser就知道了。谢谢。也许我不能用php拆分,因为我只写了部分文本,在“点/句点”之后还有更多的文本。但我用regex anwser就知道了。非常感谢。