Forum Moderators: coopster

Message Too Old, No Replies

Preg_Replace problem

I know I'm not doing this right!

         

ahmedtheking

9:18 pm on Aug 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Grrr, I'll never get the hang of reg ex!

I've got this:

$thebody = preg_replace("/(\s¦\()".$acronymsfind."(\)¦\s)/",$acronymsreplace,$thebody);

I'm trying to replace bits of body text such as 'XML' with an acronym, but I only want to replace the specific acronyms that start with either:
- a space (\s)
- a bracket (\( or \))
- a speech mark or inverted comma (\" \')

and may end with a full stop (period - \.)

I'm not doing right and I've done as much googling and php book reading but still can't do it! :(

softwareengineer99

9:57 pm on Aug 13, 2005 (gmt 0)

10+ Year Member



I tested it on my machine and it works.


$patterns = array ('/^(\s¦\\(¦"¦\')(XML)(.*)(\.)?$/');
$replace = array (' RSS \3');
$text[]="(XML is here.";
$text[]="XML is here";
$text[]='"XML is here';
$text[]='\'XML is here';
$text[]=" XML is here";
for ($i=0;$i<sizeof($text);$i++){
echo preg_replace($patterns, $replace, $text[$i])."\n";
}

Output:
RSS is here.
XML is here
RSS is here
RSS is here
RSS is here

Hope this helps.

ahmedtheking

10:11 pm on Aug 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah I see! I had a little play and I've managed to do something like this:

"/(\s¦\)¦\"¦')LDAP(\s¦\.¦\)¦\:¦,¦\"¦')/"

but how can I set the preg_replace to only change the LDAP bit and leave the space or whatever at the beginning and end of each replace?

I had the idea that I may be able to via some kind of foreach loop but then I didn't understand how to specificly find each acronym, match it against the pattern and the replace the acronym without altering the pattern properties.

Does that make any sense?

softwareengineer99

10:24 pm on Aug 13, 2005 (gmt 0)

10+ Year Member



but how can I set the preg_replace to only change the LDAP bit and leave the space or whatever at the beginning and end of each replace?

Try something like


array ('\1LDAP\2');


I had the idea that I may be able to via some kind of foreach loop but then I didn't understand how to specificly find each acronym, match it against the pattern and the replace the acronym without altering the pattern properties.

Specify all your patterns in the following format


$patterns[] = '/^(\s¦\\(¦"¦\')(XML)(.*)(\.)?$/';
$patterns[] = '/^(\s¦\\(¦"¦\')(LDAP)(.*)(\.)?$/';
$patterns[] = '/^(\s¦\\(¦"¦\')(SOMETHING)(.*)(\.)?$/';

Specify replace patterns as:


$replace[]='\1LDAP\2';
$replace[]='\1ANOTHER\2';

I haven't tested the above but it should work.
Good luck!

ahmedtheking

8:12 am on Aug 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So what does \1 and \2 mean?

I know that ^ is the start of the string and $ is the end and that? is 0 or 1 of the char, correct?

ahmedtheking

8:55 am on Aug 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah it's all good! I managed to fix it! And heres the code for everyone to share if they want a little acronym engine:


$acronymsfindarray = array(
"LDAP",
"XML",
"PHP",
"Access Keys",
"OOP",
"XHTML",
//"HTML",
//"HTM",
"CSS",
"W3C",
"WCAG",
"PDF",
"MySQL",
"ASP.NET",
"JSP",
"ActionScript",
"Lingo",
"CD",
"DVD",
"On Demand",
"CGI",
"Perl",
"Python",
"RSS",
"RDF",
"Atom",
"URL",
"URI",
"Javascript",
"Flash",
"Shockwave",
"ASP"
);

// run a loop to add the patterns
$acronymsfind = array();
foreach ($acronymsfindarray as $key => $v) {
//$acronymsfind[$key] = "/^(\s¦\(¦\"¦'¦\/)(".$v.")(-¦\)¦\"¦'¦\/¦:¦\s¦\.)?$/";
$acronymsfind[$key] = "/(\s¦\/)(".$v.")(\s¦\.¦,)?/";
//$acronymsfind[$key] = "/".$v."/";
}

$acronymsreplacearray = array(
"<acronym title=\"Lightweight Directory Access Protocol\">LDAP</acronym>",
"<acronym title=\"eXtensible Markup Language\">XML</acronym>",
"<acronym title=\"PHP: Hypertext Preprocessor (HTML-embedded scripting language)\">PHP</acronym>",
"<acronym title=\"Navigate the site without a mouse\">Access Keys</acronym>",
"<acronym title=\"Object Oriented Programming\">OOP</acronym>",
"<acronym title=\"Extensible Hypertext Markup Language\">XHTML</acronym>",
//"<acronym title=\"Hypertext Markup Language\">HTML</acronym>",
//"<acronym title=\"Hypertext Markup Language\">HTM</acronym>",
"<acronym title=\"Cascading Style Sheet\">CSS</acronym>",
"<acronym title=\"World Wide Web Consortium\">W3C</acronym>",
"<acronym title=\"Web Content Accessibility Guidelines\">WCAG</acronym>",
"<acronym title=\"Portable Document Format\">PDF</acronym>",
"<acronym title=\"Structured Query Language (database query lanquage)\">MySQL</acronym>",
"<acronym title=\"ASP.NET is the second generation of ASP and extends the technology to include the Microsoft .NET platform\">ASP.NET</acronym>",
"<acronym title=\"Java Server Pages\">JSP</acronym>",
"<acronym title=\"Macromedia Programming Language\">ActionScript</acronym>",
"<acronym title=\"Macromedia Programming Language\">Lingo</acronym>",
"<acronym title=\"Compact Disc\">CD</acronym>",
"<acronym title=\"Digital Versatile Disc (formerly Digital Video Disc)\">DVD</acronym>",
"<acronym title=\"Content (eg video, music, etc...) there when you want it\">On Demand</acronym>",
"<acronym title=\"Common Gateway Interface (web scripting facility)\">CGI</acronym>",
"<acronym title=\"Practical Extraction and Report Language\">Perl</acronym>",
"<acronym title=\"Programming Language Similer to Perl\">Python</acronym>",
"<acronym title=\"A format for syndicating news and the content of news-like sites\">RSS</acronym>",
"<acronym title=\"Resource Description Framework (much like RSS)\">RDF</acronym>",
"<acronym title=\"Much like RSS\">Atom</acronym>",
"<acronym title=\"Uniform Resource Locator (world wide web address)\">URL</acronym>",
"<acronym title=\"Uniform Resource Identifier (much like a URL)\">URI</acronym>",
"<acronym title=\"JavaScript is a basic scripting language that allows Web authors to create dynamic pages that react to user interaction\">Javascript</acronym>",
"<acronym title=\"Graphics animation computer software product by Macromedia\">Flash</acronym>",
"<acronym title=\"Graphics, 3D and GUI design animation computer software product by Macromedia\">Shockwave</acronym>",
"<acronym title=\"Active Server Page(s) (Microsoft web scripting language and file extension)\">ASP</acronym>"
);

// run a loop to add the definitions
$acronymsreplace = array();
foreach ($acronymsreplacearray as $key => $v) {
$acronymsreplace[$key] = "\\1".$v."\\3";
}
$thebody = preg_replace($acronymsfind,$acronymsreplace,$thebody);