/    Sign up×
Community /Pin to ProfileBookmark

Help with delimiters

Hello,

I am using php to take input text from an html form and output it in an xml file. I am using preg_replace to replace certain portions of content. I am having trouble though with the delimiters.

This is what I want to do
Input : “I ate lunch”
Output: “LUNCH”

As i have it set up now, the delimiters work fine if i use ‘/Today I ate lunch/’, and the replacement string as ‘lunch’

The problem i have is that I don’t understand how to use delimeters when dealing with non-alphanumeric variables. One of the lines ii need to condense into a concise version goes something like this:

Input: “I ate lunch at 5-6 (different) places w/billy this wk”

For this I’m unsure how I need to use delimiters because the forward slash is actually in the phrase. I have tried this but it doesn’t seem to work. Do i need to use a new non-alphanumeric symbol for each character?

Input: “[I ate lunch at 5**6 *(*different*)* places w*/*billy this wk]”

I hope someone can help me understand this. It would help me a lot.

Best,

-A

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 30.2010 — Make use of preg_quote().
[code=php]
$search = "text with / and (stuff) like *this*";
$result = preg_replace('/' . preg_quote($search) . '/i', 'replacement', $string);
[/code]
Copy linkTweet thisAlerts:
@AporterauthorNov 30.2010 — So is my problem in that I used ' instead of " ?

Im not sure that i understand your post. I am trying to take a large amount of text and condense certain sections of it.

So in a sense it looks like this originally:

[B]Input:[/B]

I ate lunch at 5-6 (different) places w/billy this wk

-Hamburgers

-Pasta

-Salad

I ate breakfast at 2-3 (different) places w/jim this wk

-Biscuits

-Eggs

-Pancakes

[B]Output:[/B]

Lunch

-Hamburgers

-Pasta

-Salad

Breakfast

-Biscuits

-Eggs

-Pancakes

[B]My code looks like this:[/B]

[code=php]
$inputDoc //already defined from POST data on html form. it works fine as
//far as importing the input text.

$search = array();
$search[0] = "I ate lunch at 5-6 (different) places w/billy this wk"
$search[1] = "I ate breakfast at 2-3 (different) places w/jim this wk";

$replace = array();
$replace[0] = 'LUNCH';
$replace[1] = 'BREAKFAST';

$result = preg_replace($search,$replace,$InputDoc);
[/code]


The $InputDoc is huge and I will add more search and replace values, but this is where i have had trouble with the delimiters, through trial and error I think it is because of the / in the text.
Copy linkTweet thisAlerts:
@NogDogNov 30.2010 — The problem is that in PCRE regular expressions, many characters have special meanings. To have them be treated as literal characters instead, you escape them with a back-slash, much like you do certain characters in SQL statements or even internal quotes within PHP echo() statements. The preg_quote() function will automatically escape such characters much like mysql_real_escape_string() will for MySQL string literals. Also, the regexp must begin and end with a delimiter: many different characters can be used, but "/" is the default -- if you want to use something else, then you need to let preg_quote() know what it is via its second parameter.
[code=php]
$search = array();
$search[0] = "/" . preg_quote("I ate lunch at 5-6 (different) places w/billy this wk") . "/";
// use a different delimiter, and make the regexp case-insensitive:
$search[1] = "#" . preg_quote("I ate breakfast at 2-3 (different) places w/jim this wk", "#") . "/i";
[/code]
Copy linkTweet thisAlerts:
@AporterauthorDec 02.2010 — NogDog,

That info actually helped me a ton. I've now got a almost complete script that works well and converts over 60 patterns very quickly. The big problem I'm having is with apostrophes. My script takes html POST data and formats it into a format suitable for e-mail HTML. This has become a roadblock that I can not figure a way around.

My Input as pasted into and HTML POST:
"I'd like to go hang out some day"

"No sorry I can't hang out with you"[/QUOTE]


My PHP code:

[code=php]$patterns = array ();
$patterns[0] = "#" . preg_quote("I'd like to go hang out some day") . "#";

$replacements = array();
$replacements[0] = "<strong>LETS HANG OUT</strong><br>";

$result = preg_replace($patterns, replacements, $InputDoc);[/code]


The problem I'm having is that instead of spitting out

[B]LETS HANG OUT[/B]

Sorry I can't hang out with you
[/QUOTE]

i get

I'd like to go hang out some day Sorry I can't hang out with you.[/QUOTE]


I have tried this, but it also does not work:
[code=php]$patterns[0] = "#" . preg_quote("I'd like to go hang out some day") . "#";[/code]

Can you think of a reason that the single quote/apostrophe used in the pattern array is not working? I've tried to use backslashes and alternate delimiters to no avail. For some reason it is not able to match up my pattern to the actual text due to the apostrophe, and ' does not work.

My final pass to convert all the "answer" responses from ' to single apostrophes works great and they show up fine in the html e-mail.

This has been great help and the project has been well received at work. Any ideas to help with this issue?

Thanks!

A
Copy linkTweet thisAlerts:
@AporterauthorDec 06.2010 — Problem solved!

Used a get_string_between function to find the apostrophe without actually typing it.
×

Success!

Help @Aporter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...