• 0

Need help with regex


Question

I got the following code I need to modify:


function htmlize($var){
$var = htmlspecialchars($var);
while(ereg('\^([0-9])', $var)) {
foreach(array('black', 'red', 'darkgreen', 'yellow', 'blue', 'cyan', 'deeppink', 'white', 'blue-night', 'red-night') as $num_color => $name_color) {
if (ereg('\^([0-9])(.*)\^([0-9])', $var)){
$var = preg_replace("#\^".$num_color."(.*)\^([0-9])#Usi", "<font color=\"".$name_color."\">$1</font>^$2", $var);
} else {
$var = preg_replace("#\^".$num_color."(.*)$#Usi", "<font color=\"".$name_color."\">$1</font>", $var);
}
}
}
$end = removefunchars($var);
return $end;
}[/CODE]

It is used with quake 3 which uses ^ and a number to change the colour of characters. The code above works, but I need to add funny characters such as *{}

Right now those characters are not colour coded and it skips over other characters that follow.

EDIT: It actually seems to be a bug and now I'm not sure what to do...

2Tt24.png

It seems the one guy is using letters instead of numbers to change the colours.

Here is the array:

[CODE]
[6] => Array
(
[frags] => 0
[ping] => 61
[nick] => ^p*^a{^pC^aH^pS^a}^p*^aU^pnb^aR
[gq_name] => ^p*^a{^pC^aH^pS^a}^p*^aU^pnb^aR
[gq_score] => 0
[gq_ping] => 61
)
[13] => Array
(
[frags] => 117
[ping] => 51
[nick] => ^2*{^8CHS^2}*^8^2!^8<^2!^8N^2
[gq_name] => ^2*{^8CHS^2}*^8^2!^8<^2!^8N^2
[gq_score] => 117
[gq_ping] => 51
)
[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1106349-need-help-with-regex/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

I found another code that does the same thing but is a bit more thorough and need to add $ to it.


function check_color($text, $switch)
{
$clr = array ( // colors
"\"#000000\"", "\"#FF0000\"", "\"#008800\"", "\"#E6FF00\"", // 1
"\"#0B00DB\"", "\"#2EFCFF\"", "\"#FF00F7\"", "\"#FFFFFF\"", // 2
"\"#000000\"", "\"#757575\"", "\"#FF0000\"", "\"#006E55\"", // 3
"\"#970080\"", "\"#005AFF\"", "\"#00FFFF\"", "\"#63C6FF\"", // 4
"\"#00DA2F\"", "\"#003820\"", "\"#7C0015\"", "\"#940123\"", // 5
"\"#752700\"", "\"#A7905E\"", "\"#555C26\"", "\"#B0B0B0\"", // 6
"\"#FFFFFF\"", "\"#000000\"", "\"#FF0000\"", "\"#00B906\"", // 7
"\"#FFFF00\"", "\"#0B00DF\"", "\"#00FCFF\"", "\"#FF00F7\"", // 8
"\"#FFFFFF\"", "\"#00FFFF\"", "\"#757575\"", "\"#FF8000\"", // 9
"\"#F7FF00\"", "\"#BBBBBB\"", "\"#747228\"", "\"#FF5500\"", // 10
"\"#670504\"", "\"#0000FF\"" // 11
);
if ($switch == 1)
{ // colored string
$search = array (
"/\^0/", "/\^1/", "/\^2/", "/\^3/", // 1
"/\^4/", "/\^5/", "/\^6/", "/\^7/", // 2
"/\^8/", "/\^9/", "/\^a/", "/\^b/", // 3
"/\^c/", "/\^d/", "/\^e/", "/\^f/", // 4
"/\^g/", "/\^h/", "/\^i/", "/\^j/", // 5
"/\^k/", "/\^l/", "/\^m/", "/\^n/", // 6
"/\^o/", "/\^p/", "/\^q/", "/\^r/", // 7
"/\^s/", "/\^t/", "/\^u/", "/\^v/", // 8
"/\^w/", "/\^x/", "/\^y/", "/\^z/", // 9
"/\^\//", "/\^\*/", "/\^\-/", "/\^\+/", // 10
"/\^\?/", "/\^\@/", "/\^</", "/\^>/", // 11
"/\^\&/", "/\^\)/", "/\^\(/", "/\^[A-Z]/", // 12
"/\^\_/", // 14
"/&</", "/^(.*?)<\/font>/" // 15
);
$replace = array (
"&<font color=$clr[0]>", "&<font color=$clr[1]>", // 1
"&<font color=$clr[2]>", "&<font color=$clr[3]>", // 2
"&<font color=$clr[4]>", "&<font color=$clr[5]>", // 3
"&<font color=$clr[6]>", "&<font color=$clr[7]>", // 4
"&<font color=$clr[8]>", "&<font color=$clr[9]>", // 5
"&<font color=$clr[10]>", "&<font color=$clr[11]>", // 6
"&<font color=$clr[12]>", "&<font color=$clr[13]>", // 7
"&<font color=$clr[14]>", "&<font color=$clr[15]>", // 8
"&<font color=$clr[16]>", "&<font color=$clr[17]>", // 9
"&<font color=$clr[18]>", "&<font color=$clr[19]>", // 10
"&<font color=$clr[20]>", "&<font color=$clr[21]>", // 11
"&<font color=$clr[22]>", "&<font color=$clr[23]>", // 12
"&<font color=$clr[24]>", "&<font color=$clr[25]>", // 13
"&<font color=$clr[26]>", "&<font color=$clr[27]>", // 14
"&<font color=$clr[28]>", "&<font color=$clr[29]>", // 15
"&<font color=$clr[30]>", "&<font color=$clr[31]>", // 16
"&<font color=$clr[32]>", "&<font color=$clr[33]>", // 17
"&<font color=$clr[34]>", "&<font color=$clr[35]>", // 18
"&<font color=$clr[36]>", "&<font color=$clr[37]>", // 19
"&<font color=$clr[38]>", "&<font color=$clr[39]>", // 20
"&<font color=$clr[40]>", "&<font color=$clr[41]>", // 21
"", "", "", "", "", "", // 22
"", "</font><", "\$1" // 23
);
$ctext = preg_replace($search, $replace, $text);
if ($ctext != $text)
{
$ctext = preg_replace("/$/", "</font>", $ctext);
}
return $ctext;
}
elseif ($switch == 2)
{ // colored numbers
if ($text <= 39)
{
$ctext = "<font color=$clr[7]>$text</font>";
}
elseif ($text <= 69)
{
$ctext = "<font color=$clr[5]>$text</font>";
}
elseif ($text <= 129)
{
$ctext = "<font color=$clr[8]>$text</font>";
}
elseif ($text <= 399)
{
$ctext = "<font color=$clr[9]>$text</font>";
}
else
{
$ctext = "<font color=$clr[1]>$text</font>";
}
return $ctext;
}
}
[/CODE]

EDIT: It's actually only one person I'm having trouble with now

^@^@^$iq>^@50gr

  • 0

So... I have read over both of your posts several times and I am having a little trouble figuring out exactly what you are trying to accomplish. I will explain the situation as best I can understand it, and you can correct me where I'm wrong.

You are creating a leader board, or possibly post-game score board, in PHP to display stats from your Quake 3 server. You are accepting an array of data directly from Quake 3 and trying to parse it in PHP. The problem is, some of your players have figured out exactly what you're doing and are trying to color their in-game player names, which you don't want to happen. They are doing this by logging into your Quake 3 server with user names that include HTML tags, such as <font color> (the HTML font color tag is depreciated in favor of the functionally identical CSS color property according to MDN, which is why I linked to the CSS color article), which your PHP code sending directly to the web browser to be interpreted and displayed on screen.

If my interpretation of the problem is correct, it sounds like you are failing to sanitize your input correctly. The two functions you posted above are your attempts at mitigating the issue, but each one fails to take into account some specific cases, despite the verbosity of the second function (which is not necessarily indicative of better coverage or quality). Based on this interpretation, you probably have even bigger security holes lurking in your code. This bug is annoying but fairly harmless, however, others could be worse. I recommend that you check out this article on sanitizing your inputs in PHP.

Additionally, make sure that you know what all of your code is doing! Don't just copy/paste code from the Internet without fully understanding it. That is not to say that you shouldn't use external libraries; that would be foolish. However, make sure that you understand what your library does before you use it. For example, I don't know all of the implementation details of SQLite, and I probably couldn't have written a library that high-quality or complex by myself, but I have read its documentation and understand the interface, best practices, relevant performance characteristics, and caveats. It is not just a black box that performs magic someone told me would solve my problem.

Finally, in addition to a better title and more thorough description in your initial post, please follow the forum guidelines about tagging your post.

Please use the following format when starting a new topic that relates to a single programming language

[Programming Language] Name of your topic

While it wasn't too difficult to figure out which language you are using based on the code you posted, it would have saved me a little trouble. (I looked in the tags too, which also don't exist.) Especially when you are asking for free help, it is best to make it as easy for others to help you as possible; you will probably get more responses that way. When I see poorly formatted posts with missing details, I am far less likely to take the time to respond (and I'm probably not the only one).

EDIT: This post is mostly the same as the one above it. My last post was fine immediately after I posted it. When I checked again just a couple of minutes ago, most of the post was missing. Will a mod please remove my post above this one?

  • 0

Regex isn't parsing html, the code I posted adds <font color> to the page translating the various q3 font modifiers. For instance ^1 = <font color="#FF0000">

I added htmlspecialchars to fix some issues with the php. Basically, the code is not able to parse ^$ and translate that to <font color=...>

Edited by DPyro
  • 0

Regex isn't parsing html, the code I posted adds <font color> to the page translating the various q3 font modifiers. For instance ^1 = <font color="#FF0000">

I added htmlspecialchars to fix some issues with the php. Basically, the code is not able to parse ^$ and translate that to <font color=...>

In that case, can you post a table of the codes you need to translate? The HTML color codes are standard, but I can't help you very much if I don't know the codes you are translating from.

  • 0

In that case, can you post a table of the codes you need to translate? The HTML color codes are standard, but I can't help you very much if I don't know the codes you are translating from.

See the regex in the previous post


if ($switch == 1)
{ // colored string
$search = array (
"/\^0/", "/\^1/", "/\^2/", "/\^3/", // 1
"/\^4/", "/\^5/", "/\^6/", "/\^7/", // 2
"/\^8/", "/\^9/", "/\^a/", "/\^b/", // 3
"/\^c/", "/\^d/", "/\^e/", "/\^f/", // 4
"/\^g/", "/\^h/", "/\^i/", "/\^j/", // 5
"/\^k/", "/\^l/", "/\^m/", "/\^n/", // 6
"/\^o/", "/\^p/", "/\^q/", "/\^r/", // 7
"/\^s/", "/\^t/", "/\^u/", "/\^v/", // 8
"/\^w/", "/\^x/", "/\^y/", "/\^z/", // 9
"/\^\//", "/\^\*/", "/\^\-/", "/\^\+/", // 10
"/\^\?/", "/\^\@/", "/\^</", "/\^>/", // 11
"/\^\&/", "/\^\)/", "/\^\(/", "/\^[A-Z]/", // 12
"/\^\_/", // 14
"/&</", "/^(.*?)<\/font>/"
[/CODE]

So if the name of someone in q3 was ^1D^2P^3y^4r^5o it would look like [b][color=#ff0000]D[/color][color=#00ff00]P[/color][color=#ffff00]y[/color][color=#0000ff]r[/color][color=#00ffff]o[/color][/b]

The problem shows up with a name like ^@^@^$iq>^@50gr where he is using special characters to modify the colour.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Win11Debloat 2026.06.14 by Razvan Serea Win11Debloat is a lightweight, easy to use PowerShell script that allows you to quickly declutter and customize your Windows experience. It can remove pre-installed bloatware apps, disable telemetry, remove intrusive interface elements and much more. The script also includes many features that system administrators and power users will enjoy. Such as a powerful command-line interface, support for Windows Audit mode and the option to make changes to other Windows users. All changes made by Win11Debloat can be easily reversed, and most removed apps can be restored via the Microsoft Store. A full guide on how to undo the changes is available here. Win11Debloat features: Below is an overview of the key features and functionality offered by Win11Debloat. Please refer to the wiki for more information about the default settings preset. Remove a wide variety of preinstalled apps. Click here for more info. Disable telemetry, diagnostic data, activity history, app-launch tracking & targeted ads. Disable tips, tricks, suggestions & ads across Windows. Disable Windows location services & app location access. Disable Find My Device location tracking. Disable 'Windows Spotlight' and tips & tricks on the lock screen. Disable 'Windows Spotlight' desktop background option. Disable ads, suggestions and the MSN news feed in Microsoft Edge. Hide Microsoft 365 ads on the Settings 'Home' page, or hide the 'Home' page entirely. Disable & remove Microsoft Copilot. Disable Windows Recall. Disable Click to Do, AI text & image analysis tool. Prevent AI service (WSAIFabricSvc) from starting automatically. Disable AI Features in Edge. Disable AI Features in Paint. Disable AI Features in Notepad. Disable the Drag Tray for sharing & moving files. Restore the old Windows 10 style context menu. Turn off Enhance Pointer Precision, also known as mouse acceleration. Disable the Sticky Keys keyboard shortcut. Disable Storage Sense automatic disk cleanup. Disable fast start-up to ensure a full shutdown. ...and more. Once you’ve downloaded the Win11Debloat file (Get.ps1), just follow these quick steps: Locate the Get.ps1 script file. Right-click the file and select Run with PowerShell from the context menu. If prompted by User Account Control (UAC), select Yes to grant the script the necessary administrative permissions. Win11Debloat 2026.06.14 changes: This is a minor release that hopefully addresses the false positives in Windows Defender and Bitdefender that prevented users from downloading and/or running Win11Debloat. Refactor Get-RegFileOperations.ps1 to address false positives by @Raphire in #626 Add logging around WinGet app retrieval and increase timeout to 20s by @Raphire Download: Win11Debloat 2026.06.14 | Open Source View: Win11Debloat Home Page | Screenshots 1| 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Still using Microsoft Money 2005 in 2026 here!
    • I have a couple to mention, and they still run great on Windows 11 Adobe Lightroom Version 2 Alcohol 120% CLZ Book, Comic, Game, Movie, & Music Collector (PC - No longer sold / Grandfathered in - now mobile apps/online only) DVDDecrypter ISO Buster Pro version 1.9.1 (Still supports HD-DVD too) Nero Burning Rom 8 (Only the burning software, no backup, media converter, etc)   OpenAL (Runtime) - GuildWars 1 Reforged still uses it for 3d headphone audio PowerDVD 12 Ultra SPTD (SCSI Pass through Direct Driver) UltraISO Windows Media Encoder 9 WinImage You can tell I still sport an optical drive    
    • Linux 7.1 arrives with an NTFS overhaul and major hardware performance boosts by Paul Hill The founder of the Linux kernel has just announced the availability of Linux 7.1. This is a stable version of the kernel that will now be tested by various Linux distributions before it is shipped to users through update managers. Some users, like those on Debian, for example, might not get it for a long time, if at all, while Fedora users can expect it in the near future. With Linux 7.1 out on time, the merge window for Linux 7.2 is now open, giving contributors the opportunity to send in major new features that have been waiting for the last two months. Torvalds warned that he is currently travelling and will be in another timezone, so timing for the merge window may be irregular due to timezone differences and limited internet access. Torvalds said that he has already fetched early pull requests to allow him to do some offline work, but the travel could still cause disruption. Right now, he is not planning to extend the release, but did consider it. He said he might later regret not extending, though. In terms of this last week of development for Linux 7.1, Torvalds said there were no major or alarming changes. This week consisted mostly of smaller driver updates to GPU, networking, and sound, networking fixes, trace tooling fixes, and misc minor fixes. The shortlog this week lists fixes for driver bugs, memory leaks, I/O and USB fixes, networking and RDMA fixes, DRM/graphics fixes, and tooling and verification improvements. Specific fixes include USB series heap-overflow and buffer overflow fixes, and multiple use-after-free, memory-leak, and refcount corrections across subsystems such as i2c, zram, gpio, and net. There are fixes for graphics drivers, including amdgpu, i915, and virtio, as well as hypervisor and virtualization tweaks affecting mshv, vmbus, and hyperv. According to Phoronix, anyone running Linux 7.1 should look out for the new NTFS driver, Intel FRED for improved performance on Panther Lake and future CPUs, faster graphics with Intel Arc Battlemage, and improvements for older AMD Radeon GPUs. If you are running Linux on your computer and everything is fine, then you don’t need to worry about updating to Linux 7.1 as a priority; just wait for it to be pushed to you. If you have tried Linux on hardware but it didn’t work properly, trying again with a distro that uses Linux 7.1 could cause Linux to work on your machine, thanks to the new hardware support.
    • you can also do this with this tool: PowerSettingsExplorer made by mbk1969 at 3dguru forum.. I found it by accident researching on modern standby and annoying quirks of it in 2022
  • Recent Achievements

    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
    • One Month Later
      agatameier earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      507
    2. 2
      +Edouard
      197
    3. 3
      PsYcHoKiLLa
      139
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!