Jump to content

Recommended Posts

Okay I need a little bit of help on this as strings constantly confuse me and reading tutorials actually got me a bit more loss then before. I have developed this mod that has become pretty popular and I'm debugging it. What I have are two variables.

 

$username (The user's username, no html markup in it)

$musername (The user's username, with html markup in it)

 

So for example:

 

$musername = <b><u>$username</u></b>

 

The php code above would be the equivolant to what $musername is equal to. What I need to do is create an array or two variables, whatever is easier and grab all that html markup. The first variable would be all the html or anything really before the $username variable, and the other would take all of that html after the $username variable. I need to store those html markups in either two seperate variables or an array so i can call upon it later in the script. I need this to fix a bug issue.

Link to comment
https://forums.phpfreaks.com/topic/212987-splitting-a-string/
Share on other sites

I am not fully sure what you want but if you want to echo out $musername with html + another variable in it and echo that out you can do the following:

 

$username = 'blablabla';
$musername = '<b><u>'.$username.'</b></u>'; // this is in single quotes with appending dots
$musername2 = "<b><u>{$username}</b></u>"; // in double quotes with {} around the variable
$musername3 = "<b><u>$username</b></u>"; // double quotes without {} les clear and could be problematic
echo $username.'<br />';
echo $musername.'<br />';
echo $musername2.'<br />';
echo $musername3.'<br />';

 

Link to comment
https://forums.phpfreaks.com/topic/212987-splitting-a-string/#findComment-1109301
Share on other sites

Nah you guys misunderstood me. I know how to assign variables I just typed that as an example. I have this variable called

 

$musername

 

It contains things like:

<b>$username</b>

OR

<font color="#ff0000"><u>$username</u></font>

OR

<i><u>$username</b></i>

 

etc.

 

 

What I need to do is to take one variable and assign it so that it takes only the code before whatever the $username variable is. So for example it would take

<b><i>

 

 

And then I need to assign a second variable to take everything after the $username variable in the phrase so for example it would take:

</b></i>

 

Basically I have a string and want to chop it up into 3 pieces, all the beginning html, the username and then all the closing tag html

Link to comment
https://forums.phpfreaks.com/topic/212987-splitting-a-string/#findComment-1109379
Share on other sites

hey, I have had this question myself.  I needed to make a log of IP addresses that went on my site.  The thing is that I wanted to make each new IP add a row of an existing table written in HTML.  I had to 'chop' the variable(which included the entire HTML code) into a couple of parts(header, body and footer).  The code breaks up the variable into sections and puts each section into an array.  In my HTML file, I added an HTML comment "<!--break-->" which does nothing in the web browser when ran, but this is where the pieces get chopped up by the PHP code into sections.  Then later I include the "<--break-->" comment again for further processing.  This script works really good, It got me very excited when I actually did this the first time :D. Here is my PHP code:(look for the '$pieces = explode("<!--break-->", $ffh);' part, this is the code you need)

<?php
//Create TXT of log
//Get Remote IP into $IP variable
$IP=$_SERVER['REMOTE_ADDR'];
//Display $IP variable on Client
echo "Your IP is:";echo $IP;
//Set file to be edited into variable $IPFile
$IPFile = "Data/Logs/IPLog.txt";
//Open File to be edited, "Append" Mode into $fh variable
$fh = fopen($IPFile, 'a') or die("can't open file");
//Write IP into file specified in $fh variable each line
fwrite($fh, $IP);
fwrite($fh, "   Accessed:");
fwrite($fh, date("d/m/y H:i:s",time()));
fwrite($fh, "
");
//Close $fh variable(close file)
fclose($fh);
?><?php
//Create HTML of LOG !!!No Auto Refresh inside log!!!
//Declare File to be written into variable
$IPFileHTML = "Data/Logs/IPLog.html";
//if Declare file does NOT exsist, show error message and create new file
if (!file_exists($IPFileHTML)) {
echo "<br>IPTRACELOG not found, New Log being created.";
    fopen($IPFileHTML, 'w') or die("cannot create log file, check permissions.  If you do not have permissions, contact your system administrator");}
//Get contents of that file
$ffh = file_get_contents("Data/Logs/IPLog.html");
//break contents into an array, <!--break--> identifies where to split file
$pieces = explode("<!--break-->", $ffh);
//open declared variable to be written to
$fh = fopen($IPFileHTML, 'w') or die("can't open file to write");
//write new file replacing old one every time this page is loaded
fputs($fh, "<html><title>IP TRACE LOG</title><script language='javascript' type='text/javascript'>function refresh(){window.location.href='IPLog.html';setTimeout('setTimeout()', 2000); }</script><body bgcolor='#C0C0C0'><center><h1>USAGE LOG</h1><table bgcolor='#FF8C00' border=5><tr><th>IP</th><th>Acessed Date/Time</th></tr><!--break-->");
fputs($fh, "<tr><td>");
fputs($fh, $_SERVER['REMOTE_ADDR']);
fputs($fh, "</td><td>");
fputs($fh, date("d/m/y H:i:s",time()));
fputs($fh, "</td></tr>");
fputs($fh, $pieces[1]);
fputs($fh, "<!--break--></table></center></body></html>");
fclose($fh);
?>

Hope this helps:) God Bless:)

Link to comment
https://forums.phpfreaks.com/topic/212987-splitting-a-string/#findComment-1109480
Share on other sites

try

<?php
$musername = '<font color="#ff0000"><u>Psycho</u></font>';
$name = strip_tags($musername);
$parts = explode($name, $musername);
print_r($parts);
?>

WTF?! That's such a simple and perfect solution. I just had one of those "Why didn't I think of that" moments.

Link to comment
https://forums.phpfreaks.com/topic/212987-splitting-a-string/#findComment-1109481
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.