Resource icon

Post Content Find / Replace 1.0.0

No permission to download
I tried

Code:
\[img\](https:\/\/www\.my-forum\.com)\/forum\/attachment\.php\?attachmentid=([0-9]+).*\[\/img]


for

Code:
[IMG]https://www.my-forum.com/forum/attachment.php?attachmentid=123&d=222[/IMG]

Try it out in http://regexr.com with your actual forum url. Remember to set all flags


The replacement would be

Code:
[img]\1/attachments/\2[/img]

This is a case where you can do some real damage if you don't have the replace done right. Please take precautions!
 
This results in a server error:
Server Error

preg_match_all(): Delimiter must not be alphanumeric or backslash
 
Whenever you are writing a regex you need to backslash the following / \ . [ ] ? . ^ * and some others (look up regex escaping specail characters)

In PHP you can use other delimiter characters besides / and /, such as # and #. I always use slashes so I can test it in a JavaScript driven regex tester
 
This results in a server error:
Server Error

preg_match_all(): Delimiter must not be alphanumeric or backslash
Oh, when I copied the regex from the tester it left out the delimiters (/ and /)

Code:
/\[img\](https:\/\/www\.my-forum\.com)\/forum\/attachment\.php\?attachmentid=([0-9]+).*\[\/img]/i
 
Code:
/\[img\](https:\/\/www\.my-forum\.com)\/forum\/attachment\.php\?attachmentid=([0-9]+).*?\[\/img\]/i

This is a little safer. The "?" near the end makes sure it finds the shortest possible match. Sometimes regexes overmatch and grab more than you want.
 
I've read a lot but did not find a solution for this, although I would expect it as very common.

How do I find and replace such media includes with title or URL, please?
This may occour for YouTube and Vimeo and with mixed character cases:

Code:
[YOUTUBE="This is the title of the video"]on8EBjTftcg[/YOUTUBE]
[YOUTUBE="https://www.youtube.com/watch?v=on8EBjTftcg"]on8EBjTftcg[/YOUTUBE]

[VIMEO="This is the title of the video"]236410761[/VIMEO]
[VIMEO="https://vimeo.com/236410761"]236410761[/VIMEO]

Thank you for any help.
 
Find: /\[youtube=[^\]]+\]([\S\s]+?)\[\/youtube\]/i
Replace: [MEDIA=YOUTUBE]\1[/MEDIA]


Find: /\[vimeo=[^\]]+\]([\S\s]+?)\[\/vimeo\]/i
Replace: [MEDIA=VIMEO]\1[/VIMEO]

 
I think this will get them all in one fell swoop

Find: /\[(YOUTUBE|VIMEO)=[^\]]+?\]([^\[]+?)\[\/(YOUTUBE|VIMEO)\]/i

Replace: [MEDIA=\1]\2[/MEDIA]
 
Last edited:
Thank you so much, @Nerbert. Will try in the next hour. Is this case sensitive? Anyway, I'll try and can run it with lower cases a second time!
 
I did something slightly wrong with an SQL-query and now transfered the end of the youtube embeded already.

So, the query (forget to simulate before ******, yes I have an update but want to avoid that) was:
Code:
UPDATE xf_post SET message = REPLACE(message, '[/youtube]', '[/MEDIA]');

Will this work (I really have no clue from regex)?

Code:
Find: /\[youtube=[^\]]+\]([\S\s]+?)/i
Replace: [MEDIA=YOUTUBE]\1

edit: I probably just could revert it ;)
Code:
UPDATE xf_post SET message = REPLACE(message, '[/MEDIA]', '[/youtube]');

But, the addon gave me an error 500 with this one, just simualting (not saving):

Code:
Find: /\[youtube=[^\]]+\]([\S\s]+?)/i
Replace: [MEDIA=YOUTUBE]\1

Is this faulty or does my AWS instance does not manage to replace that?

And: does you full query work for titles with and without quote marks ""?
 
Last edited:
Have you already replaced the closing tags with "[/MEDIA]"?

Don't use those regexes you posted. They will match everything after the opening youtube tag all the way to the end of the post. If you have replaced the closing tags and you can revert it, do so. Those regexes I posted must be able to match the closing tag
 
Good, that you warned me. Yes, I converted back and for safety reasons I used a fresh dump.

And: does you full query work for titles with and without quote marks ""?

Will this work for all, with and with "" with URLs and just for words?
Doing it now...
 
And, would it be possible to place the content between "" somewhere else? I would think not, but like to ask ;)
 
I would like to make all Wikipedia hotlinks unformatted text. For example
Turn this:
[URL='https://en.wikipedia.org/wiki/Ottoman_Empire']Ottoman Empire[/URL]
Into this:
Ottoman Empire

Preferably it would work also for http and for any language like
[URL='https://XX.wikipedia.org/wiki/Ottoman_Empire']Ottoman Empire[/URL]
where XX is the language code.
 
Back
Top Bottom