简体   繁体   中英

php substr not working on variable

I have a batch of code that builds a table using php variables.

What I am trying to do is capture the first 3 characters of flight.flightnum flight.flightnum returns BAW111 for example and I want the BAW bit. Easy right? Well I am now php expert but using substr is not working for me at all. Code as follows:

<script type="text/html" id="acars_map_row">
<tr class="<%=flight.trclass%>">
<td><a href="<?php echo url('/profile/view');?>/<%=flight.pilotid%>"><%=flight.pilotid%> - <%=flight.pilotname%></a></td>
<td><%=flight.flightnum%></td>
<td><%=flight.depicao%></td>
<td><%=flight.arricao%></td>
<td><%=flight.phasedetail%></td>
<td><%=flight.alt%></td>
<td><%=flight.gs%></td>
<td><%=flight.distremaining%> <?php echo Config::Get('UNITS');?> / <%=flight.timeremaining%></td>
</tr>
</script>

I have never worked with these types of variables before, I have tried:

<?php $result = "<%=flight.flightnum%>"; echo $result ?> // = BAW111

<?php $result = "<%=flight.flightnum%>"; echo substr($result, 0 ,3) ?> // gives an error and nothing is returned.

I'm sure its something obvious but im out of ideas.

The

<%=flight.flightnum%>

is processed client side so your:

echo substr($result, 0 ,3)

is putting just:

<%=

into your source (eg the first 3 characters). This is breaking the page generation because the mark up is invalid.

You will need to resolve this issue client side after what ever runs the <%=stuff%> processing.

Additionally, var_dump outputs information about a variable. It will display its value, type, and length. The 21 in this instance was the length of <%=flight.flightnum%> .. this also was outputted but whatever is running the <%= s (I thought ASP) converted it to the flight value.

You need to wrap the variables in PHP tags so that they are parsed.

Eg

<td><?php <%=flight.flightnumber%> ?></td>

They are likely appearing as HTML tags as the code is currently.

...

´$result´ is likely to contain a reference to the object. Try wrapping it in quotes first.

<?php
$result = "<%=flight.flightnum%>";
echo substr($result, 0 ,3)
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM