473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Best approach to this using preg_replace/match

i found this code out in the ng, and its seems long and clunky,
i am still experimenting with preg_match and _replace, but the
syntax is a bit confusing. it seems i always misplace or mistype
something.

this is the code that i am trying to replace with better, optimized code.
i would like to make a preg_match and then preg_replace for this, and if
a match is found, just removing it.

i started with this:
(preg_match ("/(\*\/)|(\/\*)|(\-\-)|(#|;)/", $line))
and
$line = preg_replace ("/(\*\/)|(\/\*)|(\-\-)|(#|;)/", "", $line) . ";";

and this is the total items that i am searching for:
$line = str_replace(".", " ", $line);
$line = str_replace("\"", " ", $line);
$line = str_replace("'", "", $line);
$line = str_replace("+", " ", $line);
$line = str_replace("-", " ", $line);
$line = str_replace("*", " ", $line);
$line = str_replace("/", " ", $line);
$line = str_replace("!", " ", $line);
$line = str_replace("%", " ", $line);
$line = str_replace(">", " ", $line);
$line = str_replace("<", " ", $line);
$line = str_replace("^", " ", $line);
$line = str_replace("(", " ", $line);
$line = str_replace(")", " ", $line);
$line = str_replace("[", " ", $line);
$line = str_replace("]", " ", $line);
$line = str_replace("{", " ", $line);
$line = str_replace("}", " ", $line);
$line = str_replace("\\", " ", $line);
$line = str_replace("=", " ", $line);
$line = str_replace("$", " ", $line);
$line = str_replace("#", " ", $line);
$line = str_replace("?", " ", $line);
$line = str_replace("~", " ", $line);
$line = str_replace(":", " ", $line);
$line = str_replace("_", " ", $line);
$line = str_replace(" ", " ", $line);
$line = str_replace("&amp;", " ", $line);
$line = str_replace("&copy;", " ", $line);
$line = str_replace("&nbsp;", " ", $line);
$line = str_replace("&quot;", " ", $line);
$line = str_replace("&uuml;", "ü", $line);
$line = str_replace("&Uuml;", "Ü", $line);
$line = str_replace("&", " ", $line);
$line = str_replace(";", " ", $line);
$line = str_replace("\n", " ", $line);

should i just leave all of the str_replaces ? or can preg handle all of
these ?
thanks.
Jul 16 '05 #1
1 4042
fartsniff wrote:
i found this code out in the ng, and its seems long and clunky,
Yeah, but sometimes you gotta take the long and clunky road
this is the code that i am trying to replace with better, optimized code.
See my comment below about arrays.
i would like to make a preg_match and then preg_replace for this, and if
a match is found, just removing it.
Since all the characters you are trying to replace are not part of a
simple class of characters (\s is spaces, \d is digits, etc.)

<snip lots of code>

should i just leave all of the str_replaces ? or can preg handle all of
these ?
thanks.


According to the documentation, you can use arrays with str_replace.
Something like this:

<?
$search = array("+", "-", "&amp;");
$replace = array(" ", " ", " ");

$result = str_replace($search, $replace, $line);
// or, if they are all replaced with the same thing
$result = str_replace($search, " ", $line);
?>

You build a big array at first, but it's not so hard to maintain, once
you get the initial array in place. This *seems* less clunky...

Jason

Jul 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel Kończyk | last post by:
I can't fing a way, to match any characters except the phrase 'foo'. Using + matches any character except a, b or c, but I need to not match 'foo', because f is ok, o is ok, but foo - 3 letters at...
3
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this ...
5
by: Daniel Pryde | last post by:
Hi everyone. I was wondering if anyone might be able to help me out here. I'm currently looking to find the quickest way to find a best fit match in a large array. My problem is that I have an...
16
by: bissatch | last post by:
Hi, I am trying to use preg_replace to take out all occurances of PHP code after reading (fread()) the contents of a PHP file. The code I am using is: $html = preg_replace("<?php*?>", "",...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
7
by: GoL | last post by:
Hi all, I have a problem that I *think* is simple, but since I'm completely ignorant about regex expression I don't have a clue how to do it... What I need to do is to find all instances in a...
7
by: jeddiki | last post by:
Hi, As I am in Turkey at present, I can not see vidoes on youtube. So I have tried a few proxies but keep finding them slow or not working. So I have installed myphpProxy on my server under...
4
jamwil
by: jamwil | last post by:
What up friends, I'm using this little function to parse hyperlinks in plain text and convert them into links. It goes like soprivate function hyperlink($text) { // match protocol://address/path/...
7
by: jeddiki | last post by:
Hi, I am using a function called htmlwrap() which states that it does NOT add a "<br>" to the 70 character line so that it forces a line wrap. ( the script safely wraps long words without...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.