• 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

    • Dopamine 3.0.6 by Razvan Serea Dopamine is an awesome free audio player which tries to make organizing and listening to music as simple and pretty as possible. Dopamine has been designed for Windows 7, Windows 8.x and Windows 10 and plays mp3, ogg vorbis, flac, wma and m4a/aac music formats quite well. The best part? It's created by long-time Neowin member, Raphaël Godart. If you’re looking for a music player to handle a large music collection, you should definitely give Dopamine a try. Dopamine 3.0.6 changelog: Fixed Manually edited album covers are overwritten on the next collection refresh Fixed AppImage package not working on modern GNU/Linux distributions Deleting song from playlist sometimes fails Playback controls only work when clicking on upper half of the buttons It's unclear that files must be tagged with an external ReplayGain scanner (for example rsgain) before normalization can take effect. Change to Artist or Album tags is not reflected in the song list view nor in the Now Playing information ReplayGain issues Smart playlist filters ignore text containing accents or other special characters Some MP3 files trigger an "MPEG header not found" error due to a too-narrow initial MPEG header scan range Changed Updated the Vietnamese translation Download: Dopamine 3.0.6 | 122.0 MB (Open Source) Links: Home Page | Forum Discussion | Screenshot | Other OSes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • BleachBit 6.0.1 Beta by Razvan Serea When your computer is getting full, BleachBit quickly frees disk space. When your information is only your business, BleachBit guards your privacy. With BleachBit you can free cache, delete cookies, clear Internet history, shred temporary files, delete logs, and discard junk you didn't know was there. Designed for Linux and Windows systems, it wipes clean thousands of applications including Firefox, Microsoft Edge, Google Chrome, Opera, Safari, and more. Beyond simply deleting files, BleachBit includes advanced features such as shredding files to prevent recovery, wiping free disk space to hide traces of files deleted by other applications, and vacuuming Firefox to make it faster. Better than free, BleachBit is open source. BleachBit has many useful features: Delete your private files so completely that "even God can't read them" according to South Carolina Representative Trey Gowdy. Simple operation: read the descriptions, check the boxes you want, click preview, and click delete. Multi-platform: Linux and Windows Free of charge and no money trail Free to share, learn, and modify (open source) No adware, spyware, malware, browser toolbars, or "value-added software" Translated to 64 languages besides American English Shred files to hide their contents and prevent data recovery Shred any file (such as a spreadsheet on your desktop) Overwrite free disk space to hide previously deleted files Portable app for Windows: run without installation Command line interface for scripting and automation CleanerML allows anyone to write a new cleaner using XML Automatically import and update winapp2.ini cleaner files (a separate download) giving Windows users access to 2500+ additional cleaners Frequent software updates with new features Going beyond standard deletion of files, BleachBit has several advanced cleaners: Clear the memory and swap on Linux Delete broken shortcuts on Linux Delete the Firefox URL history without deleting the whole file—with optional shredding Delete Linux localizations: delete languages you don't use. More powerful than localepurge and available on more Linux distributions. Clean APT for Debian, Ubuntu, Kubuntu, Xubuntu, and Linux Mint Find widely-scattered junk such as Thumbs.db and .DS_Store files. Execute yum clean for CentOS, Fedora, and Red Hat to remove cached package data Delete Windows registry keys—often where MRU (most recently used) lists are stored Delete the OpenOffice.org recent documents list without deleting the whole Common.xcu file Overwrite free disk space to hide previously files Vacuum Firefox, Google Chrome, Liferea, Thunderbird, and Yum databases: shrink files without removing data to save space and improve speed Surgically remove private information from .ini and JSON configuration files and SQLite3 databases without deleting the whole file Overwrite data in SQLite3 before deleting it to prevent recovery (optional) BleachBit 6.0.1 Beta release notes: BleachBit 6.0.1 beta is now available for testing. This maintenance-focused release includes bug fixes, updated translations, and a range of safe enhancements. This release fixes a Windows security issue that could allow arbitrary file deletion during privileged cleaning (reported by Zeze with TeamT5). It also adds new cleaners (including a DNS cache cleaner, Claude Code, and Visual Studio Code forks), support for multiple Chrome and Edge profiles, new deep scan options for developer directories like node_modules and venv, and safer, faster file shredding. All Platforms Added cleaners for Claude Code, DNS cache, and many Visual Studio Code forks. Added support for multiple Chrome and Edge profiles. Chrome can now clean downloaded AI models. Deep Scan can optionally remove venv, __pycache__, node_modules, and .angular directories. Deep Scan is faster by skipping directories on the keep list. File shredding is safer, faster, and leaves fewer recoverable traces. Improved handling of cookies, symlinks, Unicode filenames, external processes, and configuration files. Improved Expert Mode warnings and long warning dialogs. Fixed crashes related to cleaner detection, invalid Unicode, and malformed cleaner data. Clipboard is now cleared automatically after shredding files via paste operations. Linux Added AppImage support. Added cleaners for Visual Studio Code, Codeium, Librewolf (.deb), Transmission (Flatpak), and Profanity. Improved Linux trash detection, including Snap-installed applications and mounted drives. Fixed Wayland root CLI issues and several Snap-related problems. Improved package dependencies, AppStream metadata, and desktop file handling. Fixed startup crashes when Python Requests is unavailable. Windows Fixed a security vulnerability that could allow arbitrary file deletion when cleaning with elevated privileges. Added %WindowsSystem% variable support. Improved clipboard clearing using native Windows APIs. Improved installer experience on unsupported Windows versions. Reduced installer size and improved application robustness. Fixed Unicode handling, filename anonymization, Git revision reporting, and splash screen stability. [full release notes] Download: BleachBit 6.0 | Portable | ~20.0 MB (Open Source) View: BleachBit Home page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • DriversCloud 12.1.6 by Razvan Serea With DriversCloud (formerly My-Config.com), you can explore your computer easily, safely and free. The application quickly scans your PC and identifies the hardware and software components. DriversCloud then establishes a list of the different drivers compatible with your OS and hardware. Download the drivers needed for the proper functioning of your computer. To detect your drivers, DriversCloud also displays a detailed summary of your hardware and software configuration, analyzes your BSOD, monitors in real-time your PC voltages and temperatures and lets you share your configuration online. Once the hardware components have been detected, you will be able to obtain with just a few clicks the latest drivers corresponding to the identified hardware. You can record your configuration on the site for free, and can get the corresponding URL to post the configuration to technical forums, e-mail and social networks. You can also download the detection result (the configuration) as a PDF file. To protect the user's privacy and data confidentiality, a 4-level confidentiality system was created that filters the XML marks and gives control to the user. The default level can be modified in the preferences. Using the maximum level will prevent the user from publishing his configuration and generating a corresponding PDF file. In non-connected mode, each XML configuration is stored on the server for one day (for practical reasons). However, you are given the opportunity to manually delete it. Created in 2004, and continually improved, My-Config.com has established itself on the web as a free service to PC users running Windows and Linux operating systems. The service is designed to work with the most common Internet browsers (Edge, Firefox, Chrome, Safari). Download: DriversCloud 64-bit | 20.0 MB (Freeware) Download: DriversCloud 32-bit | 18.9 MB Link: DriversCloud Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      AndreaB earned a badge
      One Month Later
    • One Month Later
      agatameier earned a badge
      One Month Later
    • Week One Done
      agatameier earned a badge
      Week One Done
    • Week One Done
      ssd21345 earned a badge
      Week One Done
    • Contributor
      MarkHughes4096 went up a rank
      Contributor
  • Popular Contributors

    1. 1
      +primortal
      516
    2. 2
      +Edouard
      193
    3. 3
      PsYcHoKiLLa
      147
    4. 4
      ATLien_0
      96
    5. 5
      Steven P.
      77
  • Tell a friend

    Love Neowin? Tell a friend!