我正在尝试从字符串中获取指定的单词。
我不知道我是否忘记了什么,但是它仅显示前两个值?有人可以帮我吗?
感谢您的帮助!
$orgData["connector"] = 'some stringy thing SMA-female N-female FME-female';
if (preg_match_all('/(N-female|SMA-female|FME-female)/',$orgData["connector"], $matches)) {
foreach ($matches as $i => $match) {
if ($match[$i] == "N-female") {
$con1 = "|Connector: N-female";
}
if ($match[$i] == "SMA-female") {
$con2 = "|Connector: SMA-female";
}
if ($match[$i] == "FME-female") {
$con3 = "|Connector: FME-female";
}
print_r($con1 . $con2 . $con3);
}
}
答案 0 :(得分:2)
您可以使用单个变量,并在找到匹配项后追加:
@Entity
@Table(name="worktime")
public class Worktime {
@Id
@Column(name="worktime_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int worktimeId;
@Column(name="date")
@Temporal(TemporalType.DATE)
private Date date;
@ElementCollection
@CollectionTable(name="WORKTIME_TIMES")
@Temporal(TemporalType.TIME)
private List<Date> startTimes = new ArrayList<>();
@ElementCollection
@CollectionTable(name="WORKTIME_TIMES")
@Temporal(TemporalType.TIME)
private List<Date> endTimes = new ArrayList<>();
@ManyToOne
@JoinColumn(name="person_id")
private Employee employee;
...
}
请注意,我会简化模式:$orgData = 'some stringy thing SMA-female N-female FME-female';
$con = "";
if (preg_match_all('/(?:N|SMA|FME)-female/', $orgData, $matches)) {
foreach ($matches[0] as $match) {
if ($match == "N-female") {
$con .= "|Connector: N-female";
}
if ($match == "SMA-female") {
$con .= "|Connector: SMA-female";
}
if ($match == "FME-female") {
$con .= "|Connector: FME-female";
}
}
}
echo $con; // => |Connector: SMA-female|Connector: N-female|Connector: FME-female
匹配(?:N|SMA|FME)-female
,N
或SMA
,然后匹配FME
。