www.webdeveloper.com
Results 1 to 2 of 2

Thread: preg_replace warning about delimter

  1. #1
    Join Date
    May 2009
    Posts
    129

    preg_replace warning about delimter

    Hi all,

    Not very good with preg_replace and was wondering if someone could provide a quick fix to this. I know that you have to add the slashes to the delimiter, but my delimiter is a variable and I don't know how to add it to that. What I have is...

    ....
    $article_body = preg_replace ($keyword,"<a href='$url'>$keyword</a>",$article_body);
    ....

    and I get the error

    Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash

    Any help would be great. I've seen the backslashes for characters, but not for a variable. Is this even possible?

  2. #2
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    Well I'd like to point out that you aren't really making use of preg_replace!
    I would recommend you use:
    str_replace($search, $replace, $subject)

    instead, as you aren't actually using an expression.

    Example of a regular expression (NOTE: the slashes around my expression):
    PHP Code:
    <?php

    $values 
    "
    5/25/11<x>07/02/09<x>BLDG.<x>BL555<x>BL555<x>BL555<x>
    BL555<x>BL555<x>BL313<x>BL313<x>BL313<x>BL313<x>BL313<x>
    BL313<x>BL313<x>BL313<x>BL529<x>BL529<x>BL529<x>BL529<x>
    BL529<x>BL529<x>BL493<x>BL493<x>BL493<x>BL493<x>BL513<x>
    BL513<x>BL513<x>BL513<x>BL513<x>BL551<x>BL519<x>BL519d<x>
    BL519<x>BL519<x>BL539<x>BL539<x>BL539<x>BL539<x>BL523<x>
    BL551<x>BL549C<x>BL549<x>BL549G<x>BL550<x>BSC150<x>BSC150<x>
    BL547<x>BL547<x>BL547<x>BL547<x>BL543<x>BL543<x>BL543<x>
    BL543<x>BL543<x>BL543<x>BL543<x>BL433<x>BL433<x>BL433<x>
    BL433<x>BL433<x>BL433<x>BL439<x>BL439<x>BL437<x>BL441<x>
    BL453<x>BL453<x>BL453<x>BL453<x>BL455<x>BL455<x>BL455<x>
    BL456<x>BL456<x>BL456<x>BL461<x>BL461<x>BL461<x>BL462<x>
    BL462<x>BL462<x>BL463<x>BL463<x>BL463<x>BL463<x>BL463<x>
    BL454<x>BL454<x>BL454<x>BL454<x>BL454<x>BL277<x>BL277<x>
    BL277<x>BL275<x>BL275<x>BL275<x>BL276<x>BL276<x>BL276<x>
    BL276<x>BL280<x>BL280<x>BL280<x>BL272<x>BL272<x>BL272<x>
    BL271<x>BL271<x>BL304<x>BL304<x>BL304<x>BL001<x>BL001<x>
    BL299<x>This should fail!!<x>This too<x>BlahBlah9<x>BL299
    <x>BL299<x>BL299<x>BLDG. #<x>BL299<x>BL299<x>BL299<x>BL299
    <x>BL299<x>BL257<x>BL257<x>BL257<x>BL257<x>BL257<x>BL257<x>
    BL257C<x>BL257C<x>BL257C<x>BL223<x>BL223<x>BL223<x>BL221<x>
    BL221<x>BL221<x>BL227<x>BL2/(\w+) (\d+), (\d+)/i27<x>BL227
    <x>BL227<x>BL227<x>BL227<x>BL243<x>BL243<x>BL243<x>BL243<x>
    BL243<x>BL243<x>BL243<x>BL243<x>BL243<x>BL243<x>BL243<x>
    BL243<x>BL237<x>BL237<x>BL237<x>BL237<x>BL237<x>BL404B<x>
    BL045<x>BL045<x>BL045<x>BL045<x>BL043<x>BL043<x>BL043<x>
    BL047<x>BL047<x>BL047<x>BL672<x>BL672<x>BL672<x>BL672"


    $values explode("<x>"str_replace("\n",'',$values));

    foreach(
    $values as $k => $v) {
      if(
    preg_match("/^B[LSGCD]{1,2}[0-9]{3}[A-Z]{0,1}/"$v)) {
        echo 
    '<br /><span style="color:green;">'$v"</span>";
      } else {
        echo 
    '<br /><span style="color:red;">'$v"</span>";
      }
    }

    ?>
    Cheers

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles