0

So I've got a script that basically takes input from a text area and I want to find a specific word in the variable (in the string) and replace it with another.

So far I've been getting all sorts of errors.

I want to for example find **contact and replace it with **/page/contact etc.

I'll keep trying and edit in the errors that I get here. I've no idea what I'm doing wrong.

edit 1

I'm getting

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'a' in /path/.../.../*.php on line 57

When using

$string = preg_replace("**example", "**/page/example", $string);
5
  • 1
    Could you share the code? Commented Jul 30, 2014 at 21:02
  • 1
    We can't help you unless you show us the code you are using and the errors. What's wrong with a simple str_replace? Commented Jul 30, 2014 at 21:02
  • Use php.net/manual/en/function.str-replace.php Commented Jul 30, 2014 at 21:03
  • Sorry just editing my code to get the errors again. I've edited them in. Commented Jul 30, 2014 at 21:03
  • Don't use preg_replace here. You are not using a regex. Just use str_replace. Commented Jul 30, 2014 at 21:03

3 Answers 3

2

Use str_replace

$string = str_replace('**contact', '**/page/contact', $string);
Sign up to request clarification or add additional context in comments.

2 Comments

@Dan: str_replace doesn't have any "modifiers". Are you sure you tried this exactly? Note the function being used here. It's str_replace (not preg_replace, like in your code).
I'm sorry I was using preg instead of str. Oops! Very quick help, thanks guys :) Fixed now. Can't except an answer right now though, have to wait 8 more minutes..? :/
0

It would be helpful to have more context. Give us the relevant PHP code that is performing this task for you, as well as a sample of the text content that you expect to receive, in addition to how you expect to receive that content (is it sent from the web via user input? Extracted from a CSV file? Etc.)

Are you looking for a way to replace general patterns of text, or do you know exactly which text you need to be replace? For example, given the example that you have provided (replacing **contact with **/page/contact) if you can expect this exact string then you can use any of the string replacement functions that PHP provides.

If you are working with general patterns of text then you may need to use regular expressions.

1 Comment

Thanks for the input, Feek and Rocket Hazmat's answers above is what I needed. I was using preg when I needed to use str instead. I'm looking for a particular input to replace within a variable with lots of other text as well.
0

preg_replace modifiers are http://php.net/manual/en/reference.pcre.pattern.modifiers.php e,x,s,m and i

the first character in your pattern was the asterisk * followed by another then ex

**ex

which is all legitimate but the interpreter stopped at the unrecognized modifier a and showed the error.

a proper way to make this a preg_replace would be but * is also a reserved character so you need to escape it.

 preg_replace("/\*\*example/", "**/page/example", $string);

or

 preg_replace("~\*\*example~", "**/page/example", $string);

or for the **contact

 preg_replace("/\*\*contact/", "**/page/contact", $string);

3 Comments

Thanks for the input, really helpful actually :) I'd upvote but I can't just yet. Is there any benefits or best practices when it comes to using str over preg?
for simple replacements.. use str_replace if you want to flex your muscles or have some complex cases, use regex (preg_replace)
str_replace is generally faster as it is less complex

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.