Search code examples
phpsubstr

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.


Solution

  • The

    <%=flight.flightnum%>
    

    is processed client side so your:

    echo substr($result, 0 ,3)
    

    is putting just:

    <%=
    

    into your source (e.g. 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.