1

I have a big string that contains mysite/tim/here-is-something.html and other url patterns such as mysite/a/some-thing.html. Now i want to change all - to + of the first pattern and keep all other pattern intact.

Anyone know how to deal with this?

Thank you for any suggestion!

Seki
  • 11,135
  • 7
  • 46
  • 70
Pam Apple
  • 111
  • 2
  • 10
  • 1
    More information: other url patterns that i want to keep do not have `/tim/` part – Pam Apple Jun 21 '12 at 09:42
  • I assume you are talking about HTML, thus you should parse it http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – KingCrunch Jun 21 '12 at 10:06
  • Hi KingCrunch, could you give me a specific example how to do that, since i don't quite understand the link you referenced. Or can you give a link to an example how to parse such kind of thing? I'm still a newbie in this area, not sure what to search also :-(( – Pam Apple Jun 21 '12 at 10:18
  • Ok, i have archive the purpose, but it is very messy. I load all href element into an array, then do a search if an element contains `/tim/`. If yes then replace it. Then do a `str_replace` back to the original string. Is there any neater solution? – Pam Apple Jun 21 '12 at 12:04

2 Answers2

0

Sounds like you've solved it already, but the problem is neatly split into a preg_match and a str_replace. The preg_match may be more flexible than loading elements into an array as you describe you've done, but it does also requires a knowledge of regular expressions to understand how it works.

preg_replace cannot be used to achieve this result in one step because it can't handle flexible length positive-look-behinds. It can replace one occurrence of - but not multiple occurrences (unless it's put in a while loop, which would be another option).

<?
$strings = array("mysite/tim/here-is-something.html",
                 "mysite/a/some-thing.html");

foreach ($strings as $string) {
  if (preg_match("#^(mysite/tim/)(.*)#",$string,$matches)) {
    print "Changed string: $string\nto: ";
    print $matches[1].str_replace("-","+",$matches[2])."\n";
  } else {
    print "Didn't change string: $string\n";
  }
}

Output:

Changed string: mysite/tim/here-is-something.html
to: mysite/tim/here+is+something.html
Didn't change string: mysite/a/some-thing.html
Highly Irregular
  • 38,000
  • 12
  • 52
  • 70
0
$pattern = '!((mysite/tim/)[-\w\.]*)+(?=((mysite/a/)+)|(\s+|\n))!';
$html = 'mysite/tim/here-is-something.html   mysite/tim/here-is-something.html  mysite/a/here-is-something.html mysite/tim/here-is-som-ething.html  mysite/a/here-is-something.html mysite/tim/he-re-is-so-m-eth-ing.html  mysite/a/here-is-something.html  mysite/a/here-is-something.html mysite/tim/here-is-something.html  mysite/tim/here-is-something.html 
mysite/tim/here-is-something.html ';
preg_match_all($pattern,$html, $match);
//print_r($match);
foreach ($match[0] as $value) {
    $modified_value=str_replace("-","+", $value);
    $html=str_replace($value,$modified_value,$html);
}
print_r ($html);

Output:

mysite/tim/here+is+something.html mysite/tim/here+is+something.html mysite/a/here-is-something.html mysite/tim/here+is+som+ething.html mysite/a/here-is-something.html mysite/tim/he+re+is+so+m+eth+ing.html mysite/a/here-is-something.html mysite/a/here-is-something.html mysite/tim/here+is+something.html mysite/tim/here+is+something.html mysite/tim/here+is+something.html

Found matches:

match[0] => Array
        (
            [0] => mysite/tim/here-is-something.html
            [1] => mysite/tim/here-is-something.html
            [2] => mysite/tim/here-is-som-ething.html
            [3] => mysite/tim/he-re-is-so-m-eth-ing.html
            [4] => mysite/tim/here-is-something.html
            [5] => mysite/tim/here-is-something.html
            [6] => mysite/tim/here-is-something.html
        )
Alexey
  • 7,127
  • 9
  • 57
  • 94
  • great! Thank you, Alexey, but i read your answer too late since i proceeded with simple HTML. Just wondering which method is more resource efficient. – Pam Apple Jul 23 '12 at 14:56