Page 1 of 1

Simple textual replacement question

Posted: Mon Jul 14, 2008 7:34 pm
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.)

Re: Simple textual replacement question

Posted: Mon Jul 14, 2008 8:14 pm
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

Re: Simple textual replacement question

Posted: Fri Jul 18, 2008 4:43 pm
by hyle
Thanks, Mate!

Re: Simple textual replacement question

Posted: Fri Jul 18, 2008 7:47 pm
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?

Re: Simple textual replacement question

Posted: Sat Jul 19, 2008 5:20 pm
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

Re: Simple textual replacement question

Posted: Sat Jul 19, 2008 7:28 pm
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.