0

I am doing a data import from one CMS to another. I have got image callouts in one CMS, for example:

{image id="12321" align="right" crop="square"}

or

{image id="12321" align="left" crop="rectangle"}

Technically speaking, the values in align or crop could be a variety of values, all alphabetic in nature. I would like to take either of these scenarios, and turn them into the following:

[image id="12321" align="VALUEHERE" crop="VALUEHERE"]

Sorry, I should have elaborated earlier. This is part of body copy in an article. I can't just replace the first and last characters :) Also, there are callouts that are similar in nature, str_replace won't work. I was hoping for some preg_replace help, which is why I mentioned regex in the title. I should have elaborated more from the initial question post.

4
  • 5
    preg_replace() Commented Aug 2, 2012 at 20:35
  • Why do you want the quotes to be unbalanced in the final result? Is that intentional? Commented Aug 2, 2012 at 20:37
  • Also, what do your curly braces and brackets mean? Are these literally strings that you want to use are or these supposed to represent data structures (objects/arrays)? Commented Aug 2, 2012 at 20:38
  • 2
    Why do people feel compelled to ask "Did you consider looking at the PHP docs?" How is that helpful? If he doesn't know what he's looking for, how is he going to find an answer in the docs? Commented Aug 2, 2012 at 20:43

4 Answers 4

2

If all of the values are alphabetic (can't contain {) you don't even need a regex.

$finalStr = str_replace('{', '[', $initial);
$finalStr = str_replace('}', ']', $initial);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes if what he is showing is literally just string here wants to changes then the only difference is switching the curly braces for brackets which would not require regex at all.
2

The regex solution is :

$out = preg_replace('/\{(.*?)\}/','[$1]',$in);

Edit :

I will try to do my best to explain this to you, but you can read the preg_replace documentation for better explanation.

The first parameter is the regular expression, I tell PHP with it to capture anything inside curly braces (.* means 0 or more caraters). you need to wrap what you want to capture using parentheses ().

the second parameter is the replacement. you can use in it $ and a number (like I used $1), to insert what you captured in the first parameter.

So basicaly I told it to capture what is inside {} and put it inside [].

Hope that you understood something ... it is realy difficult to explain regular expressions ...

2 Comments

Better than my version that replaced each attributes value separately.
Yazmat, can you explain how this works? While I appreciate the code, I'd like to understand it even more so :)
1

Given that the format is always the same:

$input = '{image id="12321" align="left" crop="rectangle"}';
$output = preg_replace('/{image id="([^"]*)" align="([^"]*)" crop="([^"]*)"}/', '[image id="$1" align="$2" crop="$2"]', $input);

3 Comments

@Madbreaks: Yeah, you are right. It would be possible to just replace the brackets without modifying the content.
Of course, this is the sort of pattern you would use if you actually had to get those values instead of just switching the braces.
this is most appropriate for the context of what i'm doing. thanks!
1

Given that it's the first and last characters of the string,

$input[0] = "[";
$input[strlen($input)-1] = "]";

Comments

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.