I know this question has more of a WordPress background to it, but I'm hoping it's just my lack of PHP knowledge that is the problem here.
I have a regex that looks for <a>
tags such as: <a href="http://website.co.uk/post/post-title">Find Out More</a>
, which are generated from content inputted by the user. Once it finds this content a foreach
loop runs over the matched text and uses a WordPress function $postId = url_to_postid( $url );
to convert the URL into a PostID.
This is an example output: <a href="#171">Find Out More</a>
.
This works fine as long as there is one link in each piece of matched text. However if there are two or more links, it sets every link to have the same href
which is incorrect.
I'm sure this is something to do with how I've got my loop running. The code I'm using is below:
<?php
$string = get_field('sample_text_box');
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
preg_match_all($pattern, $string, $matches);
$urls = $matches[0];
foreach($urls as $key => $url) {
$postId[$key] = url_to_postid( $url );
}
$newstring = preg_replace($pattern , $postId[$key] , $string);
echo $newstring;
?>
The line get_field('sample_text_box');
is an Advanced Custom Field function which "returns the value of the specified field". You can read about it here if it helps: https://www.advancedcustomfields.com/resources/get_field/
Thanks!