I am a weird issue regarding my class property here
I have the following:
$this->tableData = '<table>';
$this->tableData .= $string;
echo $this->tableData => output <table>
I want to concatenate more string to my $this->tableData but it seems like nothing is added.
I know $string is not null and contains characters
Did I do something wrong here?
Thanks!
To see if your string is not null you should use var_dump() or print_r() functions.
Example:
$this->tableData = '<table>';
echo "Dumping tableData: " . var_dump($this->tableData);
$this->tableData .= $string;
echo "Dumping tableData 2: " . var_dump($this->tableData);
echo "Dumping string: " . var_dump($string);
That way you will see exactly what is going on.
Is your variable $string containing a HTML tag, something like <p></p> or else ?
This could be "hidden" if you print_r it inside a browser.
Related
I had an value in database like "demo text" . I want to display this content from the db in the view page as
Html code that i am using is like this <h2>Demo<span>Text</span></h2> , is there any solution for seperate each words and use one for h2 and other for span. I am using php codeigniter for the project , I don't know that whether the way i explained my problem is correct or not .
Yes you can so it with explode
if you have stored at least 2 words with space. try following
$demo ="demo text";
$arr = explode(" ",$demo);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
EDIT
If you have more words and want to split first word only you can pass limit parameter in explode
$demo ="Pligrimage to Marian Shrines";
$arr = explode(" ",$demo,2);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
I think you are looking for something like this!
$your_string = "Hello Houston! We have a problem!";
$my_array = explode(" ",trim($your_string));
$output = "<h2>";
foreach($my_array as $a_word){
if ($a_word === reset($my_array))
$output .= $a_word;
else
$output .= " <span>". $a_word . "</span>";
}
$output .= "</h2>";
print $output;
$response var has a component called custom_test_name which looks like below:
[Test_Name]ad.no.check1.check2.check3
and here is the small PHP code:
<?php
echo "<class="."com.tests.".$response["custom_test_name"][1]."</class>";
?>
This prints the <class=com.tests.[</class>..check first character [ of custom_test_name and similarly echoing of ["custom_test_name"][2] prints T, [3] prints e....However, how to print/echo only the specifics in this case?. For eg. echoing just this ad.no.check1.check2.check3 and eliminating out that [Test_Name]. Is there a way we can specify the range/some other approach?
If custom_test_name is always going to begin with [Test_Name] you can remove it with something like
$trimmed = str_replace('[Test_Name]', '', $response->custom_test_name);
echo "<class="."com.tests." . $trimmed . "</class>";
If it's not always going to be like that but is going to start with [something], you can use something like
$trimmed = preg_replace("/(\[.*\])/", '', $response->custom_test_name);
echo "<class="."com.tests." . $trimmed . "</class>";
I'm trying to echo a phpstring-message. This php string consists of html and php variables and comes form a database and i can't change that data.
$name = 'John';
$str = '<b>Hi {$name},</b><br/>How are you?';
echo $str;
So i'm trying to replace the php string, but it doesn't work. This is my code:
$str = str_replace('{', '\' . ', $str);
$str = str_replace('}', ' . \' ', $str);
I get: <b>Hi' . $name. ',</b><br/>How are you?
How do i get the string like this?
<b>Hi John,</b><br/>How are you?
Thank you in advance
Just do it like this, you won't be able to replace it with a concatenation:
echo str_replace('{$name}', $name, $str);
EDIT:
If you don't know the name of the variable just use this:
echo preg_replace('/\{(.*?)\}/', $name, $str);
it's already implemented in PHP, you can directly write the variable in double quote like this:
echo "<b>Hi $name,</b><br/>How are you?";
or for some more complex variables:
echo "<b>Hi {$user->name},</b><br/>How are you?";
I am trying to create html content in PHP and for onclick event I have included a function named uchat for a div. The function takes a name parameter which is a string.
Like below:
$name = "Php string";
$echostr .= "<div onClick='uchat(\'$name\')'>
</div>";
But, passing a string value like this causes syntax error when div is clicked. Because, single quote is within a single quote. I have tried to escape it, but it still doesnt work.
The error is this:
SyntaxError: illegal character
uchat(\
I am not sure how to escape a string parameter and I have come across this problem so many times, Please help if you have a solution for this.
Thanks.
Escaped single quotes will conflict with outer ones:
$echostr .= "<div onClick=\"uchat('$name')\">
</div>";
Here are 2 clean and simple ways to do this:
1. Classic concat
$name = "Php string";
$str = "<div onClick=\"uchat('" . $name . "')\"></div>";
print $str;
2. Using sprintf (http://us3.php.net/manual/en/function.sprintf.php)
$name = "Php string2";
$str = sprintf("<div onClick=\"uchat('%s')\"></div>", $name);
print $str;
try like this
$echostr .= "<div onClick='uchat("$name")'></div>";
This works:
<?php
$name = "Php string";
$echostr .= <<< EOF
<div onClick="uchat('$name')"></div>
EOF;
echo $echostr;
?>
Output:
<div onClick="uchat('Php string')"></div>
In order to avoid escaping all double quotes and to make the html code more readable you can use EOF.
See it action : http://ideone.com/vRCCVH
So I have the following PHP code for a registration form:
<?php
$entries = array(
0 => $_POST['signup_username'],
1 => $_POST['signup_email'],
2 => $_POST['signup_password']);
$entries_unique = array_unique($entries);
$entries_unique_values = array_values($entries_unique);
echo " <br />".$entries_unique_values. " ";
?>
... And I'm realizing my echo syntax is wrong. How could I echo the different values of my array, without assigning a variable to each of my keys (there are a number of reasons as to why I can't do that)? I'd rather not use the r_print function as well.
Thanks in advance!
How do you want to output them? Comma-separated? Each on its own line? You have plenty of options. This should do the trick for comma-separated:
echo " <br />".implode(', ', $entries_unique). " ";
That said, be careful just outputting user input directly in HTML. This will leave you wide open to XSS vulnerabilities and invalid HTML in general. To output user input in HTML safely, you need to properly HTML encode the output. This would be preferable to the line above:
echo " <br />".implode(', ', array_map('htmlspecialchars', $entries_unique)). " ";
See implode(), array_map(), and htmlspecialchars().
Take a look at php's var_export().
var_export — Outputs or returns a parsable string representation of a variable
Try the implode() function:
echo implode(', ', array_values($entries));
he foreach loop is really easy for arrays, especially single associate arrays.
foreach($entries_unique as $key => $value) {
echo "key: " . $key . " - value: " . $value . "<br/>";
}
Check out php.net: http://php.net/manual/en/control-structures.foreach.php