preg_match 一行中的所有出现
preg_match all the occurrences in a line
示例(文件=xref.tex):
This is a example string and first line with <xref>id1</xref>then,<xref>id2</xref>and with no line breaks<xref>id3</xref>.
This is a second line which has <xref>id4</xref>
示例(文件=id):
id1 eqvalue1
id2 eqvalue2
id3 eqvalue3
id4 eqvalue4
要求:每个唯一的id都有一个等价的值。我需要在 "xref.tex" 文件中每次出现时替换 id 的等效值。
目前已尝试:
$xref=file("xref.tex");
$idfile=file("id");
for($y=0;$y<count($xref);$y++){
for($z=0;$z<count($idfile);$z++){
$idvalue=explode(" ",$idfile[$z])//exploding based on space charac
$id1=$idvalue[0]; //this is equivalent value of unique id
$id2=$idvalue[1]; // this is unique id
preg_match( '/<xref>(.*?)<\/xref/', $xref[$y], $match );
//getting the content between "<xref>"and "</xref>"
if($match[1]===$id2{
$xref[$y]=str_replace($match[1],$id1,$xref[$y]);}
//here first occurrence of id is replaced. how to replace
//second occurrence of id in a line as
//preg_match( '/<xref>(.*?)<\/xref/', $xref[$y], $match )
//this regex focusing on first occurrence only every time.
//???? prob here is how can i do this logic in all the occurrences
//in each line
}
}
}
预期输出:
This is a example string and first line with <xref>eqvalue1</xref>then,<xref>eqvalue2</xref>and with no line breaks<xref>eqvalue3</xref>.
This is a second line which has <xref>eqvalue4</xref>
将文件 "id" 读取为 space 分隔的 csv 到一个数组,然后使用该数组和 preg_replace 在另一个文件上作为字符串使用 file_get_contents.
试试这个:
$re = "/(<xref>[^\d]+)(\d)(<\/xref)/m";
$str = "This is a example string and first line with <xref>id1</xref>then,<xref>id2</xref>and with no line breaks<xref>id3</xref>. This is a second line which has <xref>id4</xref>";
$subst = "eqvalue";
$result = preg_replace($re, $subst, $str);
这是我的理解。文件内容xref.tex如下
<xref>id1</xref><xref>id2</xref><xref>id3</xref><xref>id4</xref> //line 1
<xref>id2</xref><xref>id3</xref> //line 2
<xref>id4</xref> //line 3
... and so on
首先,您必须修复正则表达式。您在结尾处缺少 >
。应该是
/<xref>(.*?)<\/xref>/
然后你需要使用 preg_match_all
而不是建议的 preg_match
。
我稍微修改了代码。如果您在一行中重复相同的 ID,这也应该有效。
$xref=file("xref.tex");
$idfile=file("id");
for($y=0;$y<count($xref);$y++)
{
preg_match_all( '/<xref>(.*?)<\/xref/', $xref[$y], $match ); //get all matches and store them in *match*
for($z=0;$z<count($idfile);$z++)
{
$idvalue=explode(" ",$idfile[$z]);
$id1=$idvalue[0];
$id2=$idvalue[1];
//Below, we're replacing all the matches in line with corresponding value. Edit: Maybe not the best way, but it will give you an idea.
foreach($match[0] as $matchItem)
$xref[$y]=str_replace($matchItem,$id1,$xref[$y]);
}
}
编辑
您可能需要查看 preg_replace。我认为这将是一个更好的解决方案。
示例(文件=xref.tex):
This is a example string and first line with <xref>id1</xref>then,<xref>id2</xref>and with no line breaks<xref>id3</xref>.
This is a second line which has <xref>id4</xref>
示例(文件=id):
id1 eqvalue1
id2 eqvalue2
id3 eqvalue3
id4 eqvalue4
要求:每个唯一的id都有一个等价的值。我需要在 "xref.tex" 文件中每次出现时替换 id 的等效值。
目前已尝试:
$xref=file("xref.tex");
$idfile=file("id");
for($y=0;$y<count($xref);$y++){
for($z=0;$z<count($idfile);$z++){
$idvalue=explode(" ",$idfile[$z])//exploding based on space charac
$id1=$idvalue[0]; //this is equivalent value of unique id
$id2=$idvalue[1]; // this is unique id
preg_match( '/<xref>(.*?)<\/xref/', $xref[$y], $match );
//getting the content between "<xref>"and "</xref>"
if($match[1]===$id2{
$xref[$y]=str_replace($match[1],$id1,$xref[$y]);}
//here first occurrence of id is replaced. how to replace
//second occurrence of id in a line as
//preg_match( '/<xref>(.*?)<\/xref/', $xref[$y], $match )
//this regex focusing on first occurrence only every time.
//???? prob here is how can i do this logic in all the occurrences
//in each line
}
}
}
预期输出:
This is a example string and first line with <xref>eqvalue1</xref>then,<xref>eqvalue2</xref>and with no line breaks<xref>eqvalue3</xref>.
This is a second line which has <xref>eqvalue4</xref>
将文件 "id" 读取为 space 分隔的 csv 到一个数组,然后使用该数组和 preg_replace 在另一个文件上作为字符串使用 file_get_contents.
试试这个:
$re = "/(<xref>[^\d]+)(\d)(<\/xref)/m";
$str = "This is a example string and first line with <xref>id1</xref>then,<xref>id2</xref>and with no line breaks<xref>id3</xref>. This is a second line which has <xref>id4</xref>";
$subst = "eqvalue";
$result = preg_replace($re, $subst, $str);
这是我的理解。文件内容xref.tex如下
<xref>id1</xref><xref>id2</xref><xref>id3</xref><xref>id4</xref> //line 1
<xref>id2</xref><xref>id3</xref> //line 2
<xref>id4</xref> //line 3
... and so on
首先,您必须修复正则表达式。您在结尾处缺少 >
。应该是
/<xref>(.*?)<\/xref>/
然后你需要使用 preg_match_all
而不是建议的 preg_match
。
我稍微修改了代码。如果您在一行中重复相同的 ID,这也应该有效。
$xref=file("xref.tex");
$idfile=file("id");
for($y=0;$y<count($xref);$y++)
{
preg_match_all( '/<xref>(.*?)<\/xref/', $xref[$y], $match ); //get all matches and store them in *match*
for($z=0;$z<count($idfile);$z++)
{
$idvalue=explode(" ",$idfile[$z]);
$id1=$idvalue[0];
$id2=$idvalue[1];
//Below, we're replacing all the matches in line with corresponding value. Edit: Maybe not the best way, but it will give you an idea.
foreach($match[0] as $matchItem)
$xref[$y]=str_replace($matchItem,$id1,$xref[$y]);
}
}
编辑
您可能需要查看 preg_replace。我认为这将是一个更好的解决方案。