Search Regex Plugin

download

Download: Search Regex
Version: 1.4.11
Supports: WordPress 2.9.2 – 3.1.1
Other: WordPress.org | SVN
Support: Forum

Search Regex adds a powerful set of search and replace functions to WordPress. These go beyond the standard searching capabilities, and allow you to search and replace almost any data stored on your site. In addition to simple searches you have the full power of PHP’s regular expressions at your disposal.

The driving force behind this plugin is to aid in website migration and upgrading. If you’ve ever done either of these then you know the pain of having to go back through all your data, changing URL paths and fixing things. With this plugin you can reduce this work to a search and replace pattern and the job is finished in seconds.

Why would you want this? The primary reason for the plugin was to aid in relocating directories. It’s a real pain to manually go through every post and change image directory names. With this plugin all that was required was a simple search and replace pattern, and the job was finished in a few seconds.

Features include:

  • Simple search and replace
  • Advanced regular expression search and replaces, including back references and capture groups
  • Search previews with inline replacing
  • Perform searches in post & page content, excerpt, titles, URL, and meta-data, as well as comment content and author data
  • Full support for WordPress roles. You can only search and replace in posts you have access to
  • Fully localized

Get the Flash Player to see this movie.

Installation

Installation is just like any WordPress plugin:

  • Download Search Regex
  • Unzip
  • Upload directory to /wp-content/plugins on your server
  • Activate the plugin
  • Use Search Regex from the Manage/Search Regex menu

You can find full details of installing a plugin on the plugin installation page.

Usage

Searches can be performed from the Search Regex submenu, contained within the main Manage menu of the administration interface. Once there you will be presented with the following interface:

Search Regex interface

The search and replace patterns are, as you would expect, where you enter what you want to search for and what you want to replace it with. Several options exist to refine the searching capabilities, which will be explained later. You can also chose the data source.

When you have entered your patterns you need to press one of the search buttons:

  • Search – Just do a search and show the results
  • Replace – Perform a search and replace, but only show the results. The database is not changed.
  • Replace & Save – As Replace, but the replacements are saved back into the database.

It should be noted that while you can enter any search pattern in a simple search, you must adhere to regular expression syntax when the regular expression mode is enabled. The plugin will detect any regular expression errors and warn you.

If you wish to remove a search pattern (i.e. search for ‘cheese’ and delete the any occurrences) then you can leave the replacement pattern empty.

Search Options

The following options modify the search:

  • Regex – Enable regular expressions. Note that the search pattern can now only contained properly escaped characters. The @ character is used as a delimiter and also needs escaping.
  • Case-less – Case insensitive search
  • Multi-line – Enables multi-line mode (see PHP’s pattern modifiers page)
  • Dot-all – Enables the dot-all mode (see PHP’s pattern modifiers page)

Results

Search results as presented as a list of found occurrences. Each result is shown in context with the surrounding data and, if enabled, the replacement context is also shown.

Preview

Depending upon the data source you have options to view, edit, or replace the data. Additionally, if you double-click on the highlighted search or replace patterns then an inline edit box will appear allowing you to directly edit the data.

Inline Replace

Examples

The simplest case is changing paths:

  /wp-images/

to

  /images/

A more complicated regular expression version to only move PNG images:

  /wp-images/(.*?).png

to

  /images/png/$1.png

Warning

You should always use the Search & Replace preview before saving to the database. I will accept no responsibility for any damage caused to your data. If you are in any doubt then backup your database first.

Support & Bugs

Support and feature requests should be made through the Search Regex support forum. You can contact me directly but I spend a very limited amount of time on direct support and you stand a better chance of being answered through the forum.

If you find the plugin useful then please do consider making a donation – it is appreciated and helps towards the maintenance of the plugin.

Thanks!

















264 comments

  1. Hi,

    I’m getting this error when I activate the plugin…

    Fatal error: Call to undefined function: is_admin() in /var/www/html/blog/wp-content/plugins/search-regex/search-regex.php on line 11

    Any ideas?

    StudioGlyphic

  2. I can use this on the small comments database, but on the posts/pages, it’s possibly timing out. in safari i get:

    “Safari can’t open the page “…/wordpress/wp-admin/edit.php?page=search-regex.php” because it could not load any data from this location”

    anyway to over-ride this or is there a limitation in the plugin architecture

  3. Just to let you know, i’m currently hacking it by chopping up the database by ID range with

    SELECT ID, post_content, post_title FROM wp_posts where ID>0 and ID < 5000 order by ID

    and that’s working out for now. but obv the timeout would be better to over-ride

  4. I want to replace following from each post:
    some src code

    With
    <code>some src code</code>

    What search and replace pattern I need to use

    I can replace with <code> but then how do I match with </code> as post have more tag located

  5. It really depends on your source code. If you have something unique at the start and end of the code then you can do something like:

    Search: stringatstart(.*?)stringatend
    Replace: <code>$1</code>

    Regular expressions are an enormously complicated subject, so you may be better of looking for a tutorial on them.

  6. Hey,

    This plug-in just saved my life. Wow! Was having to edit hundreds of posts by hand to fix bad html and this plugin just allowed me to fix all of them in less than 30 seconds.

    Thank you so much!

    Will

  7. Thanks for this plugin! Especially the opportunity to check out the results before saving any real change is great.

    Only three things:
    –I’d love to be able to replace text also from titles…

    –If a blog is in a secondary folder (for example yoursite.com/blog) the links to edit the posts in the result list won’t work…

    –If I put code as the replace pattern, searchregex will add the backslash characters, and they will remain even into the edited post, causing banal code such

  8. ps. the third problem become obvious if you want to substitute a simple word, such as ‘blog’ with a piece of code that wasn’t there before, such as .
    The result will be
    and the picture won’t show.

  9. sorry, third comment in a row…
    I tried to understand the problem to no avail. Anyway I am pretty sure there is something wrong with the use of htmlentities.
    In fact, using the double quote in the replace pattern will not only result with actually *seeing* the backslashes inside the edit post page. When I go to check the final code, I find that all the backslashes and the quotes are converted into html entities (that’s why they won’t go away and any replacement that adds code with quotes gives broken results).
    Somehow the data must be fired up into the database with html entities attached and not actual backslashes. I don’t understand where this happens though.

    I noticed that you use htmlentities both in the form fields and in the replace function so, it could be there. But I’m no real expert (I’m a total newbie actually).
    ( and in fact I am going to download my database and again perform all replacements with a text editor for now ) 🙁

  10. Hi there,

    using wpmu 1.1 and I have no luck using your plugin. What I am trying to do is the following:
    I had to change the plugin which did my seo optimizing for me, the old one was adding metadata to each post called head_description and as content it put the posts or pages description in there. the new plugin does not use head_description but plain and simple description…

    so I thought I could search for head_description and replace by description, but when I search in metadata it tells me´: nothign found….

    is this the wrong way? do I have to use phpmyadmin for this kind of search?

  11. Thanks for the plugin – I wanted to copy all my IMG tags with just the ALT attributes to have TITLEs too, equal to the ALT, and your plugin did it for me in just a few seconds!

    I have a few other SEO-related mass edits I want to do too.

  12. I’m a little confused and I hope you can help me. I use regex and multiline to do this:

    Search: \n\nTEXT

    To this:

    Replace: TEXT\n\n

    But the result comes out to TEXT\n\n as actual characters rather than linebreaks. Can you help?

  13. i get this error when i try to use regex search

    Fatal error: Call to a member function on a non-object in /home/########/public_html/############/wp-content/plugins/search-regex/search-regex.php on line 50

    im using wordpress 2.1.2

    plugins:
    Adsense-Deluxe
    Counterize II
    Dagon Design Form Mailer
    External Links
    Google (XML) Sitemaps
    lbcd78 Meta Keyword Generator
    Peter’s Custom Anti-Spam
    Postie
    Search Regex
    WordPress Database Backup
    WordPress Reports
    WP-PostRatings
    WP-PostViews

  14. Hi,
    Sorry for a dumb question:
    If I have in a post a link to a picture as such:

    and I wish to just change the directory to /wp/wp-content/uploads/fleurs/
    If I enter into Search Regex
    Search string /iris/wp/wp-content
    Replace with /wp/wp-content
    then the search and replace visualisation is correct, but when telling the plug in to do the replace, the result is:

    Why the insertion of the back-slash everywhere?
    Thanks in advance for explanations and advice!

    I’m working with WP 2.12
    Kind Regards
    Martin

  15. Eeeks John, my comment has been stripped of its substance!
    the essence is that the final result is that a backslash is inserted before every occurence of a double-quote
    so that the final result is backslash-double-quote/wp/wp-content

  16. Hi John,
    I get the same error as Phil.

    plugin: 1.4.1
    WP: 2.1.2
    PHP: 4.4.2

    I’m on Dreamhost, so I also tried changing the PHP version to 5.2.1, but I got the same results.

    Any help would be greatly appreciated!

  17. hey,

    when i try to activate the plugin this is what i get: Fatal error: Call to undefined function: wp_enqueue_script() in /usr/share/wordpress/wp-content/plugins/search-regex/search-regex.php on line 33

    i am using WP2.0.10

    thanx in advance,

    kumpri

  18. Whaooo!!!
    I DO LIKE your GREAT JOB ! This plugin is a MUST and is so FAST !!!
    You really deserve donations ! I’ll remember that 😉

  19. Oh, my gosh, this is just fantastic! And I agree with BAS. Your plugins aren’t just really functional — they’re very, very pretty. And that goes a long way, seriously.

    Up until now, I’d been using a plugin called Search & Replace. In a word: "ugg". No care to warning the user of what it was about to do. Let’s just say, thank goodness I’d made a database backup ahead of time.

    Yours is 10, 50, 1000 times more powerful. I only wish I’d found yours long ago! As I’m admittedly a "tweaker", i.e., I tend to change plugins more than I should, I have to change the associated tag codes. This is perfect for such a thing.

    Thanks!

  20. I’m testing the plugin now and noticed that the comment results list returns links to generic archive pages, not the post pages. For instance, I have one comment #32371, but the "view post" link points to the monthly archive it belongs to.

    Could this be a permalink setup issue?

  21. Great plugin! But there’s something odd with the options!? In my case the dot all option didn’t work (php5.2). I changed

    function set_regex_options ($dotall, $case, $multi)
    {
    $this->regex = true;
    if (!empty ($dotall))
    $this->regex_options .= 's';

    if (!empty ($case))
    $this->regex_options .= 'i';

    if (!empty ($multi))
    $this->regex_options .= 'm';

    }

    to

    function set_regex_options ($dotall, $case, $multi)
    {
    $this->regex = true;
    $this->regex_options .= 'ism';

    }

    That works for me!
    Maybe it’s the order!? ‘ism’ and not ‘sim’? Didn’t have time to have a deeper look into it 😉

  22. Hmm, I’m experiencing some difficulty with searches on a bunch of unicode corruption throughout my blog. Somewhere along the line (probably during a WP update), some unicodes like "é" got replaced by "é". Very odd and annoying. Enter Search Regex!

    What I’m seeing though is that if I do a Search and manually change the instances that are found, or if I do a Replace and approve each instance replacement, neither actually save into the database. It appears to work (no errors reported), but no change either.

    Now if I do a Replace & Save, that will work. But of course it’s a little bit scarier!

    Any ideas why?

  23. Hi Rob,

    Yep, that’s because the ‘search’ and ‘search and replace’ only show you the results (allowing you to view the results first). If you want the data to be changed in the database then you need to use the ‘replace and save’ button.

    Thanks Stanley, there may well be an issue with the order of the options!

  24. John, I’m referring to double-clicking the red highlighted search results to enter manual edit mode. When I then change the text and click the save button, the little Mac-like spinning wheel does its thing and then that search result disappears as if my change "took". But visiting that page proves otherwise.

    Seems like the opposite behavior I’d expect.

  25. […] Search Regex: Diyelimki PC Labs’daki gibi arÅŸivinizde 2000 yazı var ve bu yazıların hepsinde “/images” olarak verilen resim yollarını “/resim/” olarak deÄŸiÅŸtirmek istiyorsunuz. Tek tek elinizle düzeltmeyi düşünmüyorsunuz umarım Regex desteÄŸi de olan bu eklenti, yazı içerisinde belirli öbekleri deÄŸiÅŸtirmek istediÄŸinizde kullanabileceÄŸiniz can kurtarıcı bir eklenti. Favorim diyebilirim. […]

  26. Rob, I understand you now and yes, that is not what is expected. Trying the same thing on my site I find that the text is replaced as expected, leading me to suspect that an error occurred when you save. If you have FireFox and Firebug installed you’ll be able to see this error by clicking on the request and then clicking on the response.

  27. Hi John, your plugin is pretty good, but i found a little problem:

    The Source from ‘Comment author URL’ seems don’t include the trackbacks and pingbacks, but i really want to replace something of them.

    And another suggestion, could you add a Source such as ‘All Content’? i think i can save more time 🙂

  28. Pingback: My Life Pixelated
  29. José, yes, it should be fine replacing many posts.

    Sparanoid, ping and trackbacks enabled. Not sure what you mean about an ‘all content’

  30. John, I finally ran some tests. I didn’t have that plugin yet installed. Wow! Firebug is great! Very revealing, sadly, of some of the errors I regularly get with other WP plugins. 😮

    So, Search Regex isn’t getting any errors at all during the process of double-clicking a red highlighted search result and editing manually. After the save, that search result grays out as if the manual edit "took" but another search reveals that what I edited didn’t really get saved into the database. Odd.

    On another note, the links to the "Edit" and "View" in each search result are now pointing correctly. I resaved my permalink structure and that fixed all those links. I didn’t catch that in your documentation, but you might make note of it.

  31. hi John,

    i just upgraded Search Regex from version 1.2 to 1.4.3 and noticed the source options had been greatly expanded. does this mean that it’s not possible to search all sources simultaneously?

    and if not, how much work would it be to enable Search Regex to search all sources simultaneously?

  32. The "readme.txt" inside the downloaded search-regex.zip says "…Upload `audit-trail` directory to your `/wp-content/plugins` directory.".

    But there isn’t any ‘audit-trail’ folder in the zip.

    Did I miss something?

  33. Rupert, a copy-and-paste error! The correct directory is ‘search-regex’.

    David, it was never possible to search all sources at the same time. I’ll have a look at doing this, but suspect it may involve some structural changes.

  34. […] Search Regex adds a powerful set of search and replace functions to WordPress. These go beyond the standard searching capabilities, and allow you to search and replace almost any data stored on your site. In addition to simple searches you have the full power of PHP’s regular expressions at your disposal. […]

  35. Oh this was awesome too – helped me strip out all the / markup I had put in my posts from WP 1.5 after upgrading to WP 2.3 where tag support is integrated. Saved me from an ugly clean-up job, cheers!

  36. John, This seems like the ideal plugin for some work I need to do but I’m having some problems. I can search post titles for whatever but when I try to search post content the page churns for a little while then claims to be done but leaves me with a blank screen (just the admin header showing). My blog has around 500 posts. Any ideas?

    WP 2.3.1, plugin 1.4.3.

  37. hey John,

    i have what seems to be a bug to report:

    when performing a regex search with "\b": /\bcow\b/

    the correct results are returned, but the search field changes to /bcowb/ (notice the missing escape characters) after the search completes. intuitively i expect the same search string to remain in the search field. is this behavior intentional?

    P.S.
    is there a bug tracker for this plugin?

  38. John,

    can the output of search-regex be displayed in reverse-chronological order?

    and can the output be kept to 50 results/page?

  39. David, I’ve fixed that bug and add the ability to change the order direction, as well as put a faux-limit on the number of results (this limits the number of posts/comments searched, not the number of results returned). I haven’t done any paging as this would require a major change to the plugin.

    Rob, try the new version and putting a limit on the results. A blank page is typically a sign of no more memory.

  40. on Nov 6, 2007 5:16 am, John (author) wrote:

    > David, I’ve fixed that bug and add the ability
    > to change the order direction,

    thank you.

    > as well as put a faux-limit on the number of
    > results (this limits the number of posts/comments
    > searched, not the number of results returned).

    i don’t grok the distinction you are attempting to convey. could you re-explain using different words?

    > I haven’t done any paging as this would require a
    > major change to the plugin.

    ok

  41. Regards the faux-limit. Due to the way the search works the limit is on the number of posts that are looked at, not the number of results. The distinction is because the searching is done inside the plugin, not in the database. This means that although only 10 posts may be looked at, each post may generate hundreds of search hits. This isn’t ideal, but is certainly the cheapest and quickest method given the current plugin architecture.

  42. the "Limit to:" (a.k.a. ‘faux-limit’) options are 10, 25, 50, 100, or ‘No limit’.

    in what order is the faux-limit (chronological, reverse-chronological, random) and is it complete (i.e. assuming the faux-limit is 100 and the order is reverse-chronological, then the 100 most recent entries would be searched)?

  43. The order is whichever direction the ‘order by’ field is set to, and if you set it to 100 ascending then it will be the first 100 posts, and 100 descending it will be the last 100.

  44. while i really like the reverse sorting option, i need to downgrade to 1.4.3 because of a permissions issue.

    with 1.4.3 non-administrators could use Search Regex. with 1.4.4 only admins have access. this is a very important detail for me. many of my users require the ability to run regex searches (but not replace routines).

    with 1.4.3 i modified the Search Regex interface to hide the replace functions so my users could happily perform regex searches without the risk of accidentally executing the replace command.

    how can Search Regex access again be made available to non-admins?

    one solution would be to integrate Search Regex with the ‘Role Manger’ plugin. this would allow Search Regex access to become a capability that could be added to a particular role.

  45. Hmm, 1.4.4 is not returning all results for me. I can do a post content search for a known keyword ("Coppermine" for example) and get no results. Yet, in the WP Manage Posts search, this keyword does come up in one post.

    I’ve also tried searching in all the Sources just to be sure. Got no results, but I did get this error in the Post Meta Value:

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘asc LIMIT 0,10’ at line 1]
    SELECT wp_postmeta.meta_id AS meta_id, wp_postmeta.meta_value AS meta_value, wp_postmeta.post_id AS post_id, wp_posts.post_title AS title FROM wp_postmeta,wp_posts WHERE wp_postmeta.post_id = wp_posts.ID asc LIMIT 0,10

  46. I have run into the same problem as Rob with 1.4.4, its not returning all of the results to me 🙁 I have no limit selected for my search and it wont return all the results, another issue i am having is searching for the string ??? in some posts, i have tried with and without regexp on with an escaped query string, but nothing i do will return matches, when there are at least a dozen of these strings in the database. Any ideas what might be the cause?

  47. […] Search Regex Plugin: 讓管理者可在單一頁面搜尋-取代要修改的內容,彈指之間改掉大量資料。它甚至還可接受 regular expression。 […]

  48. i’m seeing some issues similar to Rob and Cameron with version 1.4.4.

    maybe we could pool our resources and pay John some money to take a look at the bugs in the latest version.

    anyone interested?

    and John, how much money would you need to have a go at the bugs in 1.4.4?

    peace,
    david

  49. Pingback: Perraco
  50. […] Search Regex – adds a powerful set of search and replace functions to WordPress. These go beyond the standard searching capabilities, and allow you to search and replace almost any data stored on your site. In addition to simple searches you have the full power of PHP’s regular expressions at your disposal. […]

  51. David, I’ve made available a ‘search_regex_read’ and ‘search_regex_write’ capability. Administrators can do everything, while other users can only perform searches if they have the read capability, and can only make replaces when they have the write.

    Rob, I’ve fixed the meta value error.

    Rob, David, and Cameron, I’ve tried dozens of searches and all results seem to be returned. If someone can send me a temporary login and details of a search that isn’t returning all the results then I’ll take a look (specifically I’ll need the search phrase and the posts that aren’t being reported).

    Cameron, searching for ‘???’ returns results for me. Again, if you can point me in the direction of an example then I’ll be able to investigate further.

  52. […] позволяет посетителям выставлять рейтинг постов. 58. Search Regex – плагин добавляет функцию продвинутого поиска на […]

Leave a comment

Your email address will not be published. Required fields are marked *