0

let's say i have a string like this:

$string = 'Lorem Ipsum available, <a href="##article-6##">but the majority</a> have suffered alteration in some form, by injected humour.';

Notice the <a href="##article-6##">but the majority</a>

Using this i want to link my articles based on their id's from the database.

Now, how can a perform a preg_replace on that string to see if i have something like ##article-6## and extract only that number?

Is this idea a good one? Can i do it in a better way?

EDIT:

I did it like this:

$post_content = preg_replace_callback('/##article-(\d+)##/', function($matches){$args = explode(',', $matches[1]); // separate the arguments
return call_user_func_array('article_seo_url', $args); /* pass the arguments to article_seo_url function*/ }, $post_content);

Using this code i can also replace multiple url's

3
  • 1
    Why not do the direct link when create the string? Commented Oct 14, 2012 at 21:01
  • please escape your inner quotes. Commented Oct 14, 2012 at 21:01
  • I want to do that to be sure i always have the link to the correct url in case i change the structure of the url's. Commented Oct 14, 2012 at 21:10

3 Answers 3

1

If you want to do a preg_replace, you'd want to use a regex such as /##article-(\d+)##/ and do something like this:

$string = 'Lorem Ipsum available, <a href="##article-6##">but the majority</a> have suffered alteration in some form, by injected humour.';
$string = preg_replace('/##article-(\d+)##/', 'link/to/article/with/id/$1.html', $string);

would result in $string being:

Lorem Ipsum available, <a href="link/to/article/with/id/6.html">but the majority</a> have suffered alteration in some form, by injected humour.
Sign up to request clarification or add additional context in comments.

2 Comments

yes. that outputs what you've said but if i use $string = preg_replace('/##article-(\d+)##/', article_seo_url($1), $string); it doesn't work. I get this error: Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in...
even i didn't used exactly your code i accepted your answer because it is the most similar with the final code i used.
0

If your href is stored in a variable like so:

$href="http://www.domain.com";

You can just insert that directly into the string, like so:

$string = "Lorem Ipsum available, <a href=\"$href\">but the majority</a> have suffered alteration in some form, by injected humour.";

Please note that it is important you either escape your literal quotes or use a different kind, in this case singles.

2 Comments

No, that string is extracted from the database. In that way i want to link another articles based on their id. I want to do that to be sure i always have the link to the correct url in case i change the structure of the url's. I thought is a good idea to do that.
In that case, please try Rick Calder's solution
0

If the ID is always 3 digits you don't even need regex.

$string = 'Lorem Ipsum available, <a href="##article-6##">but the majority</a> have suffered alteration in some form, by injected humour.';
$start = strpos($string, 'article-');
$articleId = substr ( $string , intval($start)+8 , 3 );

4 Comments

Yeah, copying and pasting is bad. Sorry about that.
first you forgot ;. second: this code shows me a error: Fatal error: Call to undefined function int() in...
Fixed, sorry I don't use those functions often, was off the top of my head.
... okay do the math then. I told you I don't use these functions often. instead of +1 make it +8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.