简体   繁体   English

如何从PHP的字符串中提取特定信息?

[英]How Do You Extract Specific Information Out Of A String In PHP?

I have a list of items that are in this format: 我有以下格式的项目列表:

05/01 – Some Basic: Text Here (Alpha, Bravo, Charli) 05/01 –一些基本知识:此处的文字(Alpha,Bravo,Charli)

The first part is a date, then its always followed by a - . 第一部分是日期,然后始终跟一个-。 Then some text which represents a title or location of some sort and then some keywords that may or may not be in the parentheses. 然后是一些表示标题或位置的文本,然后是可能在括号中或可能不在括号中的一些关键字。

I want to be able to extract the $month the $day the $title (including any colons) and the $keyword1 $keyword2 $keyword3 if they are there. 我希望能够提取$ month,$ day,$ title(包括任何冒号)和$ keyword1 $ keyword2 $ keyword3(如果有)。 Then assign them all to individual variables like the ones I've created so I can add them into my database. 然后将它们全部分配给各个变量,例如我创建的变量,以便将其添加到数据库中。 Getting it to do one of these at a time would be a great start but ultimately I want to be able to paste in multiple items in that format(without any spaces or characters to mark the difference besides whats already there) and be able to bulk extract them. 使其一次执行其中之一将是一个不错的开始,但最终我希望能够以该格式粘贴多个项目(除了已经存在的内容之外,没有任何空格或字符来标记差异)并能够批量提取它们。

I've tried to use Preg_Match_all and things like stristr() but am having difficulty. 我尝试使用Preg_Match_all和诸如stristr()之类的东西,但是遇到了困难。 I don't really understand how to use them to get the specific parts. 我不太了解如何使用它们来获取特定部分。 For example 例如

$month = stristr($listItem, '/', true);

echo"$month<br />";

$preDay = stristr($listItem, 'The', true);
echo"$preDay<br />";

$day = stristr($preDay, '/', false);
echo"$day<br />";

This only outputs this: 这仅输出:

05

05/01 –

/01 – 

^ I can't have the / or any weird –. ^我不能使用/或任何奇怪的–。

Actual code and an explanation of how to do this would be awesome! 实际的代码和有关如何执行此操作的说明将非常出色! If you are using pregmatch I would really appreciate a breakdown of how your filtering this. 如果您使用的是pregmatch,我将不胜感激您如何过滤此功能。 Thank you much. 非常感谢

The easiest way is using explode(). 最简单的方法是使用explode()。 The explode() function provides a fast way to break strings into arrays. explode()函数提供了一种将字符串分成数组的快速方法。 See the manual: http://php.net/manual/pt_BR/function.explode.php 参见手册: http : //php.net/manual/pt_BR/function.explode.php

With this format of string, you can break it into an array as follows. 使用这种字符串格式,可以将其分成数组,如下所示。

$string = '05/01 - Some Basic: Text Here (Alpha, Bravo, Charli)';
$array = explode('-',$string);
$date = $array[0]; //Date 05/01

$array= explode('(',$array[1]);
$title = $array[0];
$tags = $array[1];

//Now you have
$date = '05/01';
$title = 'Some Basic: Text Here';
$tags = 'Alpha, Bravo, Charli)';

//Notice that the first parenthesis of tags was exploded.
//This is valid if and only if you have your string in the format
// [date] - [title] ([tags]

I don't know if this is the best way but it works. 我不知道这是否是最好的方法,但是它可以工作。 You can also try using preg_split() function. 您也可以尝试使用preg_split()函数。 This function makes a split using regular expressions. 此函数使用正则表达式进行拆分。 If you need help on regular expressions see http://www.phpf1.com/tutorial/php-regular-expression.html 如果您需要有关正则表达式的帮助,请参见http://www.phpf1.com/tutorial/php-regular-expression.html

EDIT 1 I noticed that you string contains this symbol "–" after the date. 编辑1我注意到您在日期之后的字符串中包含此符号“ –”。 This is not a hyphen as I know it. 据我所知,这不是连字符。 I use the minus signal to write it on my keyboard. 我使用减号信号将其写在键盘上。

Your simbol / mine = –- They are a little bit different. 您的simbol /我的= =-它们有些不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM