Simple textual replacement question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hyle
Forum Newbie
Posts: 3
Joined: Mon Jul 14, 2008 7:31 pm

Simple textual replacement question

Post by hyle »

Hi!

I want to catch the first occurence of any of these strings: "csharp", "c sharp", and "c-sharp" (all case insensitive)
in the title and change it to "C#". For example,

"CSharp Certification, Development, and Training at C# Online.NET (CSharp-Online.NET)"

should become

"C# Certification, Development, and Training at C# Online.NET (CSharp-Online.NET)".

I tried to change the following line unsuccessfully:

Code: Select all

<title><?php $this->text('pagetitle') ?></title>


Can anyone help this php-impaired individual? (Line above is in a MediaWiki skin.)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Simple textual replacement question

Post by alex.barylski »

str_replace should do the trick:

Code: Select all

$title = str_replace(array('CSharp', 'C-Sharp', 'C Sharp'), 'C#', $title);
The above should replace all three instance of possible C#'s with the C# replacement requested. From what I remember anyways. :P
hyle
Forum Newbie
Posts: 3
Joined: Mon Jul 14, 2008 7:31 pm

Re: Simple textual replacement question

Post by hyle »

Thanks, Mate!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Simple textual replacement question

Post by RobertGonzalez »

Be careful with what you are doing with str_replace. It will do what it says, but in your example the changed text still has a CSharp in the parentheses. Do you want that to change to C# as well?
hyle
Forum Newbie
Posts: 3
Joined: Mon Jul 14, 2008 7:31 pm

Re: Simple textual replacement question

Post by hyle »

A better example of what I want to do is to change these types of titles containing 0, 1, or more occurences of this string "CSharp" to "C#":

Code: Select all

 
CSharp FAQ: Am I missing an assembly reference
 
CSharp FAQ: What are the differences between CSharp finalizers and Java destructors
 
to these:

Code: Select all

 
C# FAQ: Am I missing an assembly reference
 
C# FAQ: What are the differences between C# finalizers and Java destructors
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Simple textual replacement question

Post by shiznatix »

Hockeys example will work for that but you must be careful because you have things like this:

(CSharp-Online.NET)

and using hockeys example that will turn into:

(C#-Online.NET)

so I think you might need to use some regex. I can't help you in that area but thats what you are going to need to do I think. Ask such a question in the regex forum so the mods don't burst into flames or whatever they do when someone posts in the wrong thread.
Post Reply