0

I have a string sequence that goes like this: <x:object*10/>. I want to replace that tag with something else that depends on what number it is. I match the tag above with /<x:object\*[0-9]+\/>/ and using preg_replace i can replace it all with the content I want. I just need that number now.

How can I get the number please?

6
  • what kind of markup is <x:object*10/> supposed to be? Commented Mar 7, 2011 at 10:30
  • It's a custom thingy from the CMS that tells the output parser to replace that with an object (Photo Gallery, VideoPlayer, AudioPlayer etc), having an ID of 10. Commented Mar 7, 2011 at 10:33
  • This is invalid XML though. Element names may not contain * characters. If you'd use well formed XML you wouldn't have to use Regex but could use a proper XML parser, like DOM. Commented Mar 7, 2011 at 11:01
  • That's true, I could just replace the star with :, but that never gets output. The parser changes that to a series of DIV elements and image tags etc. Commented Mar 7, 2011 at 11:54
  • That would be equally invalid XML. Strange parser you have there. Commented Mar 7, 2011 at 22:31

3 Answers 3

4

By capturing it:

/<x:object\*(?P<my_number>[0-9]+)\/>/

It will be captured with name "my_number"...

Full code would be sth like this:

<?php
preg_match_all(`/<x:object\*(?P<my_number>[0-9]+)\/>/`, $where_to_search, $matches);
var_dump($matches); // Dump the matches to see in detail.
?>
Sign up to request clarification or add additional context in comments.

Comments

2

Try this one :)

/<x:object\*([0-9]+)\/>/

Comments

0

better use

/<x:object\*([0-9]+)\/>/

10 Comments

Hi, why add the extra forward slash?
bcoz * have diffrenet meaning in regex and u have * in ur given string ,`` is escape character
I know it has meaning in RegEx, that's why I'm escaping with with \*.
but u want to replace only numbers
This is not a valid regular expression in PHP. /<x:object/ will be treated as the expression, and then PHP fill throw an error because * is not a valid modifier. And what purpose has ^ here?
|

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.