My code simply displays a random line from a text file,
But in my text file most of the proxies look like this: "11.15.19.15:80" I need help how to display only the address on the site, and remove the quotation marks.
<?php
$message_array = file("proxies.txt");
$message = array_rand($message_array);
echo "$message_array[$message]";
?>
All you need to do is wrap the string in a trim():
<?php
$message_array = file("proxies.txt");
$message = array_rand($message_array);
echo trim($message_array[$message], "\""); // 11.15.19.15:80
?>
Note that the second argument in trim() is needed, because your string contains the " characters, rather than uses them to denote the string itself. Adding an escaped backslash ("\"") removes the quotation marks from what is inside the string itself.
I've created a simple demonstration of this at 3v4l.org here.
I think you can use a regex like so:
preg_replace('/["]*/g', '', $message);
Use str_replace. Here is an example:
$message = str_replace('"', '',$message);
You can use the trim function if you just need to remove the double quotes:
$message = trim($message, '"');
Also str_replace:
$message = str_replace('"', '', $message);
Related
This is a little complicated to explain but I'll try my hardest, I'm trying to create a tool to edit channel descriptions for TeamSpeak 3, to do this you use a feature called channeledit.
example usage: channeledit channel_description=My\sDescription
Presumably \s = space \n = newline, is there any possible way from using a textarea to php script to have it output the line as:
My\sDescription\nWelcome\sto\smy\sServer
Rather than appearing as:
My Description
Welcome to my Server
If there is spacing or line breaks, this kills the command and stops it working. Can anyone give me a bit of help here?
Code for this is:
$name = "Test
Test
Test test test";
$ts3_VirtualServer->execute("channeledit cid=" . $current_cid . " channel_description=" . $name);
$name = "Test Test
Test test test";
(string)$newname = str_replace(' ', '\\s', $name);
$newname = urldecode(str_replace('%0A', "\\n", urlencode($newname)));
You need to escape (\) the backslash (\). %0A 's are easier to find.
My output:
Test\sTest\nTest\stest\stest
$name=STR_replace(" ","/s",$name)
$name= str_replace("\n", '\n', $name);
If you want to replace spaces and carriage returns with literal \n and \s. I would do the following:
$name = urldecode(str_replace("%0D%0A","\\n",str_replace("+","\\s",urlencode($name))));
I have problem in sending message to my client via socket,the string that I would like to send is like this "##w32,12345678,xxx,5*zy\r\n"
$msg = $_POST['comm_input']; //"##w32,12345678,xxx,5*zy\r\n"
if this is posted i get the value of $msg which is "##w32,12345678,xxx,5*zy\r\n"
but my client will not accept this kind of message..but if I manually do like this without posting the comm_input;
$testmsg = "##w32,12345678,xxx,5*zy\r\n";
It works fine,I tried to look at in firebug there is no double quotes and \r\n.and it works fine.
if I post the comm_input.and look at in the firebug there is double quotes and \r\n,how can I remove this.
You can use str_replace function to remove \r\n.
DEMO
<?php
$testmsg = "##w32,12345678,xxx,5*zy\r\n"; <-- $_POST value
$order = "\r\n";
$replace = "";
$newstr = str_replace($order, $replace, $testmsg);
echo $newstr; //outputs ##w32,12345678,xxx,5*zy
?>
using str_replace, you need to escape the \ with and extra \, hence, \r as string becomes \\r
$msg = $_POST['comm_input']; //"##w32,12345678,xxx,5*zy\r\n" ;
$new_msg = str_replace("\\r\\n", "", $msg);
Edit: to remove double quotes
$new_msg = str_replace('"', "", $new_msg);
Consider reading this article : Escape Sequence in PHP
you can use
$msg = "##w32,12345678,xxx,5*zy\r\n";
$str = rtrim($msg);
Refer trim() and rtrim()
I am trying to pass a string to a javascript function which opens that string in an editable text area. If the string does not contain a new line character, it is passed successfully. But when there is a new line character it fails.
My code in PHP looks like
$show_txt = sprintf("showEditTextarea('%s')", $test_string);
$output[] = '<a href="#" id="link-'.$data['test'].'" onclick="'.$show_txt.';return false;">';
And the javascript function looks like -
$output[] = '<script type="text/javascript">
var showEditTextarea = function(test_string) {
alert(test_string);
}
</script>';
The string that was successfully passed was "This is a test" and it failed for "This is a first test
This is a second test"
Javascript does not allow newline characters in strings. You need to replace them by \n before the sprintf() call.
You are getting this error because there is nothing escaping your javascript variables... json_encode is useful here. addslashes will also have to be used in the context to escape the double quotes.
$show_txt = sprintf("showEditTextarea(%s)", json_encode($test_string));
$output[] = '<a href="#" id="link-'.$data['test'].'" onclick="'.htmlspecialchars($show_txt).';return false;">';
Why don't you try replacing all spaces in the php string with \r\n before you pass it to the JavaScript function? See if that works.
If that does not work then try this:
str_replace($test, "\n", "\n");
Replacing with two \ may work as it will encapsulate.
I would avoid storing HTML or JS in PHP variables as much as possible, but if you do need to store the HTML in a PHP variable then you will need to escape the new line characters.
try
$test_string = str_replace("\n", "\\\n", $test_string);
Be sure to use double quotes in the str_replace otherwise the \n will be interpreted as literally \n instead of a new line character.
Try this code, that deletes new lines:
$show_txt = sprintf("showEditTextarea('%s')", str_replace(PHP_EOL, '', $test_string));
Or replaces with: \n.
$show_txt = sprintf("showEditTextarea('%s')", str_replace(PHP_EOL, '\n', $test_string));
When I try
$stg = preg_replace("/<PREPNAME>.*<\/PREP_ZIP>/s","",$stg);
It properly work and it replace the text written in that function. And I get $stg is stripped and I get remaining string.
But when I try with "\n", it empty the complete string.
$stg = preg_replace("/<PREPNAME>.*<\/PREP_ZIP>\n/s","",$stg);
I get $stg empty in second case.
Please help. Where I am missing ?
Example String :-
This is not an XML string. This is just a blob data that is stored in the database. While fetching this is just a string.
<T2_NASD_LAST_NAME1>ASDSADSA</T2_NASD_LAST_NAME1>\r\n
<T2_NASD_FIRST_NAME1>SADASD</T2_NASD_FIRST_NAME1>\r\n
<T2_SOC1>012345678</T2_SOC1>\r\n
<T2_DOBM1>02</T2_DOBM1>\r\n <T2_DOBD1>02</T2_DOBD1>\r\n
<T2_DOBY1>1984</T2_DOBY1>\r\n
<T2_NASD_LAST_NAME>ASDSADSA</T2_NASD_LAST_NAME>\r\n
<T2_NASD_FIRST_NAME>SADASD
</T2_NASD_FIRST_NAME>\r\n
<T2_NASD_MIDDLE_NAME>ASDA</T2_NASD_MIDDLE_NAME>\r\n
<T2_SOC>012345678</T2_SOC>\r\n<T2_ADR_ADRN>SADSADAS</T2_ADR_ADRN>\r\n
<T2_ADR_ADRA>DASD</T2_ADR_ADRA>\r\n<T2_ADR_ADRC>ASDSADSA</T2_ADR_ADRC>\r\n
<T2_ADR_ADRS>MI</T2_ADR_ADRS>\r\n<T2_ADR_ADRZ>11111</T2_ADR_ADRZ>\r\n
<T2_DOBM>02</T2_DOBM>\r\n<T2_DOBD>02</T2_DOBD>\r\n
<T2_DOBY>1984</T2_DOBY>\r\n\n\n\n\n\n\n\n<PREPFLAG>NONE</PREPFLAG>\n
<PREPNAME>AAAA</PREPNAME>\n
<PREPADDRESS>BBBB</PREPADDRESS>\n
<PREP_ZIP>1984</PREP_ZIP>
You must add two \ before \n and use simple quotes
<?php
$subject = <<<'LOD'
<T2_DOBY1>1984</T2_DOBY1>\r\n
<T2_NASD_LAST_NAME>ASDSADSA</T2_NASD_LAST_NAME>\r\n
<T2_NASD_FIRST_NAME>SADASD
<PREP_ZIP>NONE</PREP_ZIP>\n
</T2_NASD_FIRST_NAME>\r\n
<T2_DOBY1>1984</T2_DOBY1>\r\n
<T2_NASD_LAST_NAME>ASDSADSA</T2_NASD_LAST_NAME>\r\n
<T2_NASD_FIRST_NAME>SADASD
</T2_NASD_FIRST_NAME>\r\n
LOD;
$pattern = '~<PREP_ZIP>.*?</PREP_ZIP>\\\n~s';
echo htmlspecialchars(preg_replace($pattern, "#!!!YOUHOU!!!#", $subject));;
I need to output a string, which is basically a java code:
I have something like this:
$web = "...if (url.contains('.mp4'))..."
I need that the single quotation mark, will be a double one, and not in html code.
Is it possible to do it?
$new_str = str_replace('\'', '"', $web);
You could optional do it by modifying the actual string (note the use of \ to escape the quotation marks):
$web = "...if (url.contains(\".mp4\"))..."
You can use like this
$web = "...if (url.contains(\".mp4\"))...";
Short of doing a strtr() replacement, just escape your double quotes:
$web = "...if (url.contains(\".mp4\"))..."
See http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double for the complete list of escaping rules.
You should use this
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>
see http://docs.php.net/manual/en/function.htmlspecialchars.php