Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Using preg_replace to filter custom textarea

preg_replace expects it’s regex to be surrounded by a character. Usually that’s a slash, like this:

'/.*/'

When you do this:

preg_replace('%%PROPOSAL_LINK_URL%%', $some_text, $replacement);

preg_replace thinks the first two % are your surrounding characters, and fails because of an unrecognized modifier. This is easy to test:

$ php -a
Interactive shell

php > echo preg_replace('%%PROPOSAL_LINK_URL%%', '%%PROPOSAL_LINK_URL%%', 'here'), PHP_EOL;
PHP Warning:  preg_replace(): Unknown modifier 'P' in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. preg_replace() php shell code:1

Warning: preg_replace(): Unknown modifier 'P' in php shell code on line 1

Call Stack:
   28.9460     235264   1. {main}() php shell code:0
   28.9460     236016   2. preg_replace() php shell code:1


php > 

You have two options:

Change your patterns to something like this: /%%PROPOSAL_LINK_URL%%/ and /%%PROPOSAL_LINK%%/

$ php -a
Interactive shell

php > echo preg_replace('/%%PROPOSAL_LINK_URL%%/', '%%PROPOSAL_LINK_URL%%', 'here'), PHP_EOL;
here

Forget all about preg_replace because you don’t need it. Use str_replace or str_ireplace instead.

<?php
str_replace('%%PROPOSAL_LINK_URL%%', $proposal_link_url, $email_content);
str_ireplace('%%PROPOSAL_LINK%%', $proposal_link, $email_content);

Be aware that str_replace and it’s brethren are not safe for use on strings that contain multi-byte characters (eg. UTF-8) by default. You can change this.

NOTE: you’ve used $link_pattern twice in your code examples. I think you mean the second one to be $url_pattern.

EDIT: it appears there’s not a mb_* replacement for str_replace or str_ireplace this comment on php.net sheds some light on the issue. str_replace should work on multibyte strings, providing the encoding of the needle and haystack are the same.

Related Posts:

  1. update_post_meta() whenever custom post type is updated
  2. How can I get some of a posts meta data whilst it is still inserting?
  3. Using publish_{custom-post-type} hook for custom post type to update meta doesn’t work
  4. Create action running on trashed_post hook to modify post_meta value
  5. Show Custom Post Type meta boxes only on Page Edit
  6. Custom filter function not working with Custom post type
  7. Filter Posts from the Main Query
  8. Importing Data from a Non-WordPress database, into WP
  9. Getting thumbnails of a custom posts featured images in a page-templates metabox
  10. List Taxonomies: Don’t list taxonomy if it has no post – depending on custom post-meta?
  11. Remove Post Custom Meta Box
  12. Can’t publish custom post type – “You are not allowed to edit this post.”
  13. Custom fields for custom post type
  14. Show future events based on custom field AND order by date
  15. Meta Data for Custom Post Type not saving
  16. Check if post with same meta value exists
  17. Filter date from post meta when date is in string format
  18. Create new custom post and post category of same name
  19. Get only used meta_values
  20. If Post Meta Equals Current Title Conditional Advanced Custom Fields
  21. Is there a way to exclude posts based on meta_values?
  22. Do posts, pages and / or custom post type objects have unique ID numbers or can there be multiple objects with the same IDs?
  23. Archieve.php not loading for custom post type
  24. Submitting Custom Post Types with custom fields from Front-end form
  25. Use remove_meta_box conditionally on custom post type
  26. create custom meta box with default value
  27. Get $post->ID from a URL
  28. Displaying a div from an assigned meta_value when on a page
  29. Meta data (Tags and Categories) for Custom Posts not showing.
  30. Display different information of a custom post type
  31. Include php on a specific page template
  32. Create Array from custom post type to display a slider
  33. Setting Post Title via Meta Data returns Auto-draft
  34. wp_trash_post action hook with custom post type
  35. How to Echo Metadata Value in Currency Format
  36. Issue on Working with Metabox – Checkbox to enable Sale Items
  37. Filter posts by tax (dropdown) and meta value
  38. How do I amend form data before it is saved for a custom post type
  39. delete_post_meta() for whole CPT / multiple posts?
  40. How to get specific post meta by title or id
  41. Publish and save specific postmeta to a filtered post_type
  42. What do the numbers mean at the end of add_action(‘save_post’)…?
  43. Running a wordpress action when a custom post type term (taxonomy category) is changed
  44. How to move a post to different post type with all meta data?
  45. Why do I have to press the “Update” button twice to save my meta box values?
  46. How can I display a drop-down select of Post Names
  47. how to show records that don’t have custom meta value
  48. WP API Response does not show my registered metadata
  49. How to detect that the save_post hook is calling the callback associated to the current edit post page only
  50. $post->post_meta not pulling any post in wordpress/php
  51. Values from meta-box not on $_POST
  52. WP_Query of custom post type sorted by meta_key has unexpected results
  53. Returning a custom content types with meta values
  54. Custom post type suddenly stoped working after WordPress 5 update
  55. Batch Extract Date from post title and put into ACF custom field
  56. copy images from custom field to another custom field
  57. How to show custom field on specific custom post type posts only when filled
  58. How to handle this specific case of custom post type?
  59. Save CTP post meta different values each day without overwriting previous values
  60. get_post_meta returns NULL in front-end, but correct result in back-end
  61. Cannot obtain custom meta information
  62. Problem with adding custom post type meta to header by plugin
  63. is_main_query() never called on WP 4.4
  64. How can I output WPAlchemy repeating fields meta values in my page template?
  65. Meta box data is saved but NOT displayed in the meta box text field. Why?
  66. How to call a post’s metadata in shortcode.php?
  67. Types plugin custom post add_action hooks
  68. Save Metabox Custom Field Value
  69. Echo custom post meta from options array
  70. Value of post meta dropdown is not showing in WordPress
  71. Meta box data not saving
  72. Get all metadata for a given custom post type
  73. WP_Meta_Query object with conditionals
  74. Trying to write shortcode with get_post_meta but isn’t working
  75. Loading plugin text domain before registering post type
  76. Related posts meta_query CPT
  77. Meta box with front-end styling
  78. Sanitaizing Select Optin For Custom Post Type Metabox in WP
  79. add_action ‘init’ from inside a class for custom post types
  80. How to retrive Custom Post Type Meta Fields in Custom WP_Query
  81. Advanced custom field – posted fields from custom post type
  82. Cannot Save MetaBox Data in Custom Post Type
  83. delete duplicate meta_value with same post_id
  84. HM CMB: Post Select Field for CPT ID
  85. Search CPT Title AND Meta
  86. Getting WordPress to store 0 values for custom post type meta
  87. updating one custom meta field only
  88. Add a class to post_class if more than one post shares same meta_value_num
  89. Function to return values from metabox
  90. Add a meta field to the list of results for a custom post type
  91. best way to use custom taxonomy, post type and meta in a job system
  92. Empty meta-box returns publishdate if no value is set?
  93. Say I have a tech blog, how best would I store technical specs for a phone, if i use custom post types
  94. Building tags and archive using meta from custom post type
  95. How to order custom posts by one of the custom fields value, ‘date’?
  96. Problem Saving Custom Post Type Meta Values
  97. Filter search posts by post meta?
  98. Custom Meta Box with variable number of fields
  99. How to recover the post ID earlier in the WP admin hook action “firing” sequence?
  100. Custom fields for post or terms which don’t update on post update
Categories custom-post-types Tags actions, custom-post-types, post-meta
$wpdb->insert Giving duplicates
WordPress page content outside WordPress

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress