1

Ok, i know there are trillions of similar questions, but i'm finding it really hard to achieve this. I have some strings of this formats:

$x = '<iframe src="[File:19]"></iframe>';
$y = '<img src=[File:2212] />';
$z = '<source src="[File:42]" />';

I'm trying to get the id given after the File: and also replace the whole [File:xxx] with another string. I'm trying like the following, but it seems i can't fully understand the usage of preg_replace.

$file = ('<iframe src="[File:134]"></frame>');
$rex = "/^.*(\[File:[0-9]{1,}\])/i" ;
if ( preg_match($rex, $file, $match) ) {
    echo 'OK';
}
$file = preg_replace ($rex, "http://lala.com/la.pdf", $file);
echo "<br>".htmlentities($file)."<br>";

Could you please give me some hints on how i can do this?

Thanks in advance.

1
  • 1
    you can use preg_match_all and delete ^.*.. (should work) But actually you should use PHP DOM for this Commented Mar 13, 2012 at 14:17

3 Answers 3

1

This should do the trick:

preg_match('/\[File:(\d+)\]/i', $str, $match)

$match[0] will have the whole string, $match[1] will have just the number.
After the regex match, you can use str_replace to remove $match[0] from the string.

Example:

$x = '<iframe src="[File:19]"></iframe>';
preg_match('/\[File:(\d+)\]/i', $x, $match);
var_dump($match);

Gives:

array(2) {
  [0]=>
  string(9) "[File:19]"
  [1]=>
  string(2) "19"
}
Sign up to request clarification or add additional context in comments.

Comments

1

Change these 2 lines

$rex = "/^.*(\[File:[0-9]{1,}\])/i" ;

$file = preg_replace ($rex, "http://lala.com/la.pdf", $file);

to:

$rex = "/^(.*)\[File:[0-9]{1,}\]/i" ;

$file = preg_replace ($rex, "$1http://lala.com/la.pdf", $file);

This will capture what is before [File...] into group 1, then in the replace part, add this group (i.e. $1) in front of the replace string.

It can be rewritten as:

$rex = "/\[File:\d+\]/i" ;

$file = preg_replace ($rex, "http://lala.com/la.pdf", $file);

2 Comments

Is it just me, or is /^(.*)\[File not only a very useless bit, but also very bad for performance? /^.*\[File/ comes down to /\[File, does it not? If you don't capture it in the regex, you don't have to replace it, right?
@BerryLangerak: Yes, you're right, I've just taken the OP's regex with minimum changes.
1

This should work:

<?php
$formats[] = '<iframe src="[File:19]"></iframe>';
$formats[] = '<img src=[File:2212] />';
$formats[] = '<source src="[File:42]" />';


foreach( $formats as $format ) {

    $regex = '~\[File:(\d+)\]~';

    $replace = function( $matches ) {
        return 'http://lala.com/la.pdf?id=' . $matches[1];
    };

    var_dump( preg_replace_callback( $regex, $replace, $format ) );
}

I've created a lambda for the replacement, because I have a feeling you want to use the id after File: instead of just discarding that. Have fun with it. If you have any questions, do tell.

1 Comment

You don't need to capture the whole expression in parens. By default the whole matched region is stored in $matches[0].

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.